Write a C programs to implement UNIX system calls and file mana gement // C program to illustrate close system Call #include<stdio.h> #include<fcntl.h> int main() { // assume that foo.txt is already created int fd1 = open("foo.txt", O_RDONLY, 0); close(fd1); // assume that baz.tzt is already created int fd2 = open("baz.txt", O_RDONLY, 0); printf("fd2 = % d\n", fd2); exit(0); } Output: // C program to illustrate open system call #include<stdio.h> #include<fcntl.h> #include<errno.h> extern int errno; int main() { int fd = open("foo.txt", O_RDONLY | O_CREAT); printf("fd = %d/n", fd); if (fd ==-1) { printf("Error Number % d\n", errno); perror("Program"); } return 0; } // C program to illustrate perror #include<unistd.h> #include<fcntl.h> #include<sys/stat.h> #include<sys/types.h> #include<stdio.h> int main() { int n,fd; char buff[50]; // declaring buffer printf("Enter text to write in the file:\n"); n= read(0, buff, 50); fd=open("file",O_CREAT | O_RDWR, 0777); write(fd, buff, n); write(1, buff, n); int close(int fd); return 0; } // C program to illustrate write system Call #include<stdio.h> #include <fcntl.h> main() { int sz; int fd = open("abc.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd < 0) { perror("r1"); exit(1); } sz = write(fd, "hello\n", strlen("hello \n")); printf("called write(% d, \"hello\\n\", %d)." " It returned %d\n", fd, strlen("hello\n"), sz); close(fd); } Write C programs to demonstrate various process // C program to demonstrate use of fork() and pipe() #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> int main() { int fd1[2]; i nt fd2[2]; char fixed_str[] = "hello"; char input_str[100]; pid_t p; if (pipe(fd1) == -1) { fprintf(stderr, "Pipe Failed"); return 1; } if (pipe(fd2) == -1) { fprintf(stderr, "Pipe Failed"); return 1; } scanf("%s", input_str); p = fork(); if (p < 0) { fprintf(stderr, "fork Failed"); return 1; } else if (p > 0) { char concat_str[100]; close(fd1[0]); write(fd1[1], input_str, strlen(input_str) + 1); close(fd1[1]); wait(NULL); close(fd2[1]); read(fd2[0], concat_str, 100); printf("Concatenated string %s\n", concat_str); close(fd2[0]); } else { close(fd1[1]); char concat_str[100]; read(fd1[0], concat_str, 100); int k = strlen(concat_str); int i; for (i = 0; i < strlen(fixed_str); i++) concat_str[k++] = fixed_str[i]; concat_str[k] = '\0'; close(fd1[0]); close(fd2[0]); write(fd2[1], concat_str, strlen(concat_str) + 1); close(fd2[1]); exit(0); } } // C program to print system information #include<stdio.h> #include<stdlib.h> #include<errno.h> #include<sys/utsname.h> int main() { struct utsname buf1; errno =0; if(uname(&buf1)!=0) { perror("uname doesn't return 0, so there is an error"); exit(EXIT_FAILURE); } printf("System Name = %s\n", buf1.sysname); printf("Node Name = %s\n", buf1.nodename); printf("Version = %s\n", buf1.version); printf("Release = %s\n", buf1.release); printf("Machine = %s\n", buf1.machine); } Write C programs to demonstrate various thread related concepts. #include <stdio.h> #include <string.h> #include <pthread.h> int i = 2; void* foo(void* p) { printf("Value recevied as argument in starting routine: "); printf("%i\n", * (int*)p); pthread_exit(&i); } int main(void) { pthread_t id; int j = 1; pthread_create(&id, NULL, foo, &j); int* ptr; pthread_join(id, (void**)&ptr); printf("Value recevied by parent from child: "); printf("%i\n", *ptr); } #include <stdio.h> #include <pthread.h> /*thread function definition*/ void* threadFunction(void* args) { while(1) { printf("I am threadFunction.\n"); } } int main() { /*creating thread id*/ pthread_t id; int ret; /*creating thread*/ ret=pthread_create(&id,NULL,&threadFunction,NULL); if(ret==0){ printf("Thread created successfully.\n"); } else{ printf("Thread not created.\n"); return 0; /*return from main*/ } while(1) { printf("I am main function.\n"); } return 0; } Write C programs to simulate CPU scheduling algorithms: FCFS, SJF, Round Robin FCFS: #include<stdio.h> int main() { int AT[10],BT[10],WT[10],TT[10],n; int burst=0,cmpl_T; float Avg_WT,Avg_TT,Total=0; printf("Enter number of the process\n"); scanf("%d",&n); printf("Enter Arrival time and Burst time of the process\n"); printf("AT\tBT\n"); for(int i=0;i<n;i++) { scanf("%d%d",&AT[i],&BT[i]); } for(int i=0;i<n;i++) { if(i==0) WT[i]=AT[i]; else WT[i]=burst-AT[i]; burst+=BT[i]; Total+=WT[i]; } Avg_WT=Total/n; cmpl_T=0; Total=0; for(int i=0;i<n;i++) { cmpl_T+=BT[i]; TT[i]=cmpl_T-AT[i]; Total+=TT[i]; } Avg_TT=Total/n; printf("Process ,Waiting_time ,TurnA_time\n"); for(int i=0;i<n;i++) { printf("%d\t\t%d\t\t%d\n",i+1,WT[i],TT[i]); } printf("Average waiting time is : %f\n",Avg_WT); printf("Average turn around time is : %f\n",Avg_TT); return 0; } SJF Non-Premptive #include<stdio.h> struct process { int id,WT,AT,BT,TAT; }; struct process a[10]; void swap(int *b,int *c) { int tem; tem=*c; *c=*b; *b=tem; } int main() { int n,check_ar=0; int Cmp_time=0; float Total_WT=0,Total_TAT=0,Avg_WT,Avg_TAT; printf("Enter the number of process \n"); scanf("%d",&n); printf("Enter the Arrival time and Burst time of the process\n"); printf("AT BT\n"); for(int i=0;i<n;i++) { scanf("%d%d",&a[i].AT,&a[i].BT); a[i].id=i+1; if(i==0) check_ar=a[i].AT; if(check_ar!=a[i].AT ) check_ar=1; } if(check_ar!=0) { for(int i=0;i<n;i++) { for(int j=0;j<n-i-1;j++) { if(a[j].AT>a[j+1].AT) { swap(&a[j].id,&a[j+1].id); swap(&a[j].AT,&a[j+1].AT); swap(&a[j].BT,&a[j+1].BT); } } } } if(check_ar!=0) { a[0].WT=a[0].AT; a[0].TAT=a[0].BT-a[0].AT; Cmp_time=a[0].TAT; Total_WT=Total_WT+a[0].WT; Total_TAT=Total_TAT+a[0].TAT; for(int i=1;i<n;i++) { int min=a[i].BT; for(int j=i+1;j<n;j++) { if(min>a[j].BT && a[j].AT<=Cmp_time) { min=a[j].BT; swap(&a[i].id,&a[j].id); swap(&a[i].AT,&a[j].AT); swap(&a[i].BT,&a[j].BT); } } a[i].WT=Cmp_time-a[i].AT; Total_WT=Total_WT+a[i].WT; Cmp_time=Cmp_time+a[i].BT; a[i].TAT=Cmp_time-a[i].AT; Total_TAT=Total_TAT+a[i].TAT; } } else { for(int i=0;i<n;i++) { int min=a[i].BT; for(int j=i+1;j<n;j++) { if(min>a[j].BT && a[j].AT<=Cmp_time) { min=a[j].BT; swap(&a[i].id,&a[j].id); swap(&a[i].AT,&a[j].AT); swap(&a[i].BT,&a[j].BT); } } a[i].WT=Cmp_time-a[i].AT; Cmp_time=Cmp_time+a[i].BT; a[i].TAT=Cmp_time-a[i].AT; Total_WT=Total_WT+a[i].WT; Total_TAT=Total_TAT+a[i].TAT; } } Avg_WT=Total_WT/n; Avg_TAT=Total_TAT/n; printf("The process are\n"); printf("ID WT TAT\n"); for(int i=0;i<n;i++) { printf("%d\t%d\t%d\n",a[i].id,a[i].WT,a[i].TAT); } printf("Avg waiting time is:- %f\n",Avg_WT); printf("Avg turn around time is:- %f",Avg_TAT); return 0; } SJF-Preemptive: #include<stdio.h> struct process { int WT,AT,BT,TAT; }; struct process a[10]; int main() { int n,temp[10]; int count=0,t=0,short_P; float total_WT=0, total_TAT=0,Avg_WT,Avg_TAT; printf("Enter the number of the process\n"); scanf("%d",&n); printf("Enter the arrival time and burst time of the process\n"); printf("AT WT\n"); for(int i=0;i<n;i++) { scanf("%d%d",&a[i].AT,&a[i].BT); temp[i]=a[i].BT; } a[9].BT=10000; for(t=0;count!=n;t++) { short_P=9; for(int i=0;i<n;i++) { if(a[i].BT<a[short_P].BT && (a[i].AT<=t && a[i].BT>0)) { short_P=i; } } a[short_P].BT=a[short_P].BT-1; if(a[short_P].BT==0) { count++; a[short_P].WT=t+1-a[short_P].AT-temp[short_P]; a[short_P].TAT=t+1-a[short_P].AT; total_WT=total_WT+a[short_P].WT; total_TAT=total_TAT+a[short_P].TAT; } } Avg_WT=total_WT/n; Avg_TAT=total_TAT/n; printf("Id WT TAT\n"); for(int i=0;i<n;i++) { printf("%d\t%d\t%d\n",i+1,a[i].WT,a[i].TAT); } printf("Avg waiting time of the process is %f\n",Avg_WT); printf("Avg turn around time of the process %f\n",Avg_TAT); } Round-Robin: #include<stdio.h> struct process { int id,AT,BT,WT,TAT; }; struct process a[10]; int queue[100]; int front=-1; int rear=-1; void insert(int n) { if(front==-1) front=0; rear=rear+1; queue[rear]=n; } int delete() { int n; n=queue[front]; front=front+1; return n; } int main() { int n,TQ,p,TIME=0; int temp[10],exist[10]={0}; float total_wt=0,total_tat=0,Avg_WT,Avg_TAT; printf("Enter the number of the process\n"); scanf("%d",&n); printf("Enter the arrival time and burst time of the process\n"); printf("AT BT\n"); for(int i=0;i<n;i++) { scanf("%d%d",&a[i].AT,&a[i].BT); a[i].id=i; temp[i]=a[i].BT; } printf("Enter the time quantum\n"); scanf("%d",&TQ); insert(0); exist[0]=1; while(front<=rear) { p=delete(); if(a[p].BT>=TQ) { a[p].BT=a[p].BT-TQ; TIME=TIME+TQ; } else { TIME=TIME+a[p].BT; a[p].BT=0; } for(int i=0;i<n;i++) { if(exist[i]==0 && a[i].AT<=TIME) { insert(i); exist[i]=1; } } if(a[p].BT==0) { a[p].TAT=TIME-a[p].AT; a[p].WT=a[p].TAT-temp[p]; total_tat=total_tat+a[p].TAT; total_wt=total_wt+a[p].WT; } else { insert(p); } } Avg_TAT=total_tat/n; Avg_WT=total_wt/n; printf("ID WT TAT\n"); for(int i=0;i<n;i++) { printf("%d %d %d\n",a[i].id,a[i].WT,a[i].TAT); } printf("Average waiting time of the processes is : %f\n",Avg_WT); printf("Average turn around time of the processes is : %f\n",Avg_TAT); return 0; } Write C programs to simulate Intra & Inter-Process Communication (IPC) techniques: Pipes, Messages Queues, Shared Memor y Using Pipes: #include<stdio.h> #include<unistd.h> int main() { int pipefds[2]; int returnstatus; char writemessages[2][20]={"Hi", "Hello"}; char readmessage[20]; returnstatus = pipe(pipefds); if (returnstatus == -1) { printf("Unable to create pipe\n"); return 1; } printf("Writing to pipe - Message 1 is %s\n", writemessages[0]); write(pipefds[1], writemessages[0], sizeof(writemessages[0])); read(pipefds[0], readmessage, sizeof(readmessage)); printf("Reading from pipe – Message 1 is %s\n", readmessage); printf("Writing to pipe - Message 2 is %s\n", writemessages[0]); write(pipefds[1], writemessages[1], sizeof(writemessages[0])); read(pipefds[0], readmessage, sizeof(readmessage)); printf("Reading from pipe – Message 2 is %s\n", readmessage); return 0; } Shared Memory: /* Filename: shm_write.c */ #include<stdio.h> #include<sys/ipc.h> #include<sys/shm.h> #include<sys/types.h> #include<string.h> #include<errno.h> #include<stdlib.h> #include<unistd.h> #include<string.h> #define BUF_SIZE 1024 #define SHM_KEY 0x1234 struct shmseg { int cnt; int complete; char buf[BUF_SIZE]; }; int fill_buffer(char * bufptr, int size); int main(int argc, char *argv[]) { int shmid, numtimes; struct shmseg *shmp; char *bufptr; int spaceavailable; shmid = shmget(SHM_KEY, sizeof(struct shmseg), 0644|IPC_CREAT); if (shmid == -1) { perror("Shared memory"); return 1; } shmp = shmat(shmid, NULL, 0); if (shmp == (void *) -1) { perror("Shared memory attach"); return 1; } /* Transfer blocks of data from buffer to shared memory */ bufptr = shmp->buf; spaceavailable = BUF_SIZE; for (numtimes = 0; numtimes < 5; numtimes++) { shmp->cnt = fill_buffer(bufptr, spaceavailable); shmp->complete = 0; printf("Writing Process: Shared Memory Write: Wrote %d bytes\n", shmp->cnt); bufptr = shmp->buf; spaceavailable = BUF_SIZE; sleep(3); } printf("Writing Process: Wrote %d times\n", numtimes); shmp->complete = 1; if (shmdt(shmp) == -1) { perror("shmdt"); return 1; } if (shmctl(shmid, IPC_RMID, 0) == -1) { perror("shmctl"); return 1; } printf("Writing Process: Complete\n"); return 0; } int fill_buffer(char * bufptr, int size) { static char ch = 'A'; int filled_count; memset(bufptr, ch, size - 1); bufptr[size-1] = '\0'; if (ch > 122) ch = 65; if ( (ch >= 65) && (ch <= 122) ) { if ( (ch >= 91) && (ch <= 96) ) { ch = 65; } } filled_count = strlen(bufptr); ch++; return filled_count; } /* Filename: shm_read.c */ #include<stdio.h> #include<sys/ipc.h> #include<sys/shm.h> #include<sys/types.h> #include<string.h> #include<errno.h> #include<stdlib.h> #define BUF_SIZE 1024 #define SHM_KEY 0x1234 struct shmseg { int cnt; int complete; char buf[BUF_SIZE]; }; int main(int argc, char *argv[]) { int shmid; struct shmseg *shmp; shmid = shmget(SHM_KEY, sizeof(struct shmseg), 0644|IPC_CREAT); if (shmid == -1) { perror("Shared memory"); return 1; } shmp = shmat(shmid, NULL, 0); if (shmp == (void *) -1) { perror("Shared memory attach"); return 1; } /* Transfer blocks of data from shared memory to stdout*/ while (shmp->complete != 1) { printf("segment contains : \n\"%s\"\n", shmp->buf); if (shmp->cnt == -1) { perror("read"); return 1; } printf("Reading Process: Shared Memory: Read %d bytes\n", shmp->cnt); sleep(3); } printf("Reading Process: Reading Done, Detaching Shared Memory\n"); if (shmdt(shmp) == -1) { perror("shmdt"); return 1; } printf("Reading Process: Complete\n"); return 0; } Message Queue: /* Filename: msgq_send.c */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #define PERMS 0644 struct my_msgbuf { long mtype; char mtext[200]; }; int main(void) { struct my_msgbuf buf; int msqid; int len; key_t key; system("touch msgq.txt"); if ((key = ftok("msgq.txt", 'B')) == -1) { perror("ftok"); exit(1); } if ((msqid = msgget(key, PERMS | IPC_CREAT)) == -1) { perror("msgget"); exit(1); } printf("message queue: ready to send messages.\n"); printf("Enter lines of text, ^D to quit:\n"); buf.mtype = 1; /* we don't really care in this case */ while(fgets(buf.mtext, sizeof buf.mtext, stdin) != NULL) { len = strlen(buf.mtext); /* remove newline at end, if it exists */ if (buf.mtext[len-1] == '\n') buf.mtext[len-1] = '\0'; if (msgsnd(msqid, &buf, len+1, 0) == -1) /* +1 for '\0' */ perror("msgsnd"); } strcpy(buf.mtext, "end"); len = strlen(buf.mtext); if (msgsnd(msqid, &buf, len+1, 0) == -1) /* +1 for '\0' */ perror("msgsnd"); if (msgctl(msqid, IPC_RMID, NULL) == -1) { perror("msgctl"); exit(1); } printf("message queue: done sending messages.\n"); return 0; } /* Filename: msgq_recv.c */ #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #define PERMS 0644 struct my_msgbuf { long mtype; char mtext[200]; }; int main(void) { struct my_msgbuf buf; int msqid; int toend; key_t key; if ((key = ftok("msgq.txt", 'B')) == -1) { perror("ftok"); exit(1); } if ((msqid = msgget(key, PERMS)) == -1) { /* connect to the queue */ perror("msgget"); exit(1); } printf("message queue: ready to receive messages.\n"); for(;;) { /* normally receiving never ends but just to make conclusion /* this program ends wuth string of end */ if (msgrcv(msqid, &buf, sizeof(buf.mtext), 0, 0) == -1) { perror("msgrcv"); exit(1); } printf("recvd: \"%s\"\n", buf.mtext); toend = strcmp(buf.mtext,"end"); if (toend == 0) break; } printf("message queue: done receiving messages.\n"); system("rm msgq.txt"); return 0; } Write C programs to simulate solutions to Classical Process Synchronization Problems: Dining Philosophers, Producer-Consumer, Readers-Writers Dining Philosophers : #include<stdio.h> #include<stdlib.h> #include<pthread.h> #include<semaphore.h>