Posts

Showing posts from October, 2024

Implementation

 2. Implementation of Shared Memory and IPC Program: Server /* Shared memory server - shms.c */ #include <stdio.h> #include <stdlib.h> #include <sys/un.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #define shmsize 27main() { char c; int shmid; key_t key =2013;char *shm, *s; if ((shmid = shmget(key, shmsize, IPC_CREAT|0666)) < 0) { perror("shmget");exit(1); } printf("Shared memory id : %d\n", shmid); if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat");exit(1); } memset(shm, 0, shmsize);s = shm; printf("Writing (a-z) onto shared memory\n");for (c = 'a'; c <= 'z'; c++) *s++ = c; *s = '\0'; while (*shm != '*'); printf("Client finished reading\n"); if(shmdt(shm) != 0) fprintf(stderr, "Could not close memory segment.\n"); shmctl(shmid, IPC_RMID, 0); } Client /* Shared memory client - shmc.c */ #include <stdio.h> #inc...

Close()

 #include <stdio.h> #include<string.h> #include<stdlib.h> #include <fcntl.h> main(int argc, char *argv[]) { int fd, n, len;char buf[100]; if (argc != 2) { printf("Usage: ./a.out <filename>\n");exit(-1); } fd = open(argv[1], O_APPEND|O_WRONLY|O_CREAT, 0644);if (fd < 0) { perror(argv[1]); exit(-1); } while((n = read(0, buf, sizeof(buf))) > 0) { len = strlen(buf); write(fd, buf, len); } close(fd); }

Read()

 #include <stdio.h> #include <stdlib.h> #include <fcntl.h> main(int argc, char *argv[]) { int fd,i; char buf[100];if (argc < 2) { printf("Usage: ./a.out <filename>\n");exit(-1); } fd = open(argv[1], O_RDONLY);if(fd == -1) { printf("%s file does not exist\n", argv[1]);exit(-1); } printf("Contents of the file %s is : \n", argv[1]);while(read(fd, buf, sizeof(buf)) > 0) printf("%s", buf); close(fd); }

Open()

 #include <stdio.h> #include<stdlib.h> #include<string.h> #include <fcntl.h> main(int argc, char *argv[]) { int fd, n, len;char buf[100]; if (argc != 2) { printf("Usage: ./a.out <filename>\n");exit(-1); } fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);if(fd < 0) { printf("File creation problem\n");exit(-1); } printf("Press Ctrl+D at end in a new line:\n");while((n = read(0, buf, sizeof(buf))) > 0) { len = strlen(buf); write(fd, buf, len); } close(fd); }

OpenDir() ,ReadDir()

 #include <stdio.h> #include <dirent.h> #include <stdlib.h> main(int argc, char *argv[]) { struct dirent *dptr; DIR *dname; if (argc != 2) { printf("Usage: ./a.out <dirname>\n");exit(-1); } if((dname = opendir(argv[1])) == NULL) { perror(argv[1]); exit(-1); } while(dptr=readdir(dname)) printf("%s\n", dptr->d_name); closedir(dname); }

Start system call

 #include <stdio.h> #include <sys/stat.h> #include <stdlib.h> #include <time.h> int main(int argc, char* argv[]) {     struct stat file;     int n;     if (argc != 2) {         printf("Usage: ./a.out <filename>\n");         exit(-1);     }     // Checking if file statistics can be obtained     if ((n = stat(argv[1], &file)) == -1) {         perror(argv[1]);         exit(-1);     }     // Printing file information     printf("User id: %d\n", file.st_uid);     printf("Group id: %d\n", file.st_gid);     printf("Block size: %ld\n", file.st_blksize);     printf("Blocks allocated: %ld\n", file.st_blocks);     printf("Inode no.: %ld\n", file.st_ino);     printf("Last accessed: %s", ctime(&(file.st_atime)));     printf("Last m...

Exec

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main() {     pid_t pid;     switch (pid = fork()) {         case -1: // Fork failed             perror("Fork failed");             exit(-1);         case 0: // Child process             printf("Child process\n");             execl("/bin/date", "date", NULL); // Execute the "date" command             exit(0);         default: // Parent process             wait(NULL); // Wait for the child to terminate             printf("Child Terminated\n");             exit(0);     } }

Wait system call

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main() {     int i, status;     pid_t pid;     pid = fork(); // Create a new process     if (pid < 0) {         // Fork failed         printf("\nProcess creation failure\n");         exit(-1);     }      else if (pid > 0) {         // Parent process         wait(NULL); // Wait for the child process to finish         printf("\nParent starts\nEven Nos: ");         for (i = 2; i <= 10; i += 2)             printf("%3d", i);         printf("\nParent ends\n");     }      else if (pid == 0) {         // Child process         printf("Ch...