27
Unix Lab IPC Using Message Queues #include<stdio.h> #include<sys/ipc.h> #include<string.h> main() { int id; struct { int type; char a[20]; }s; key_t k=984171582; s.type=1; id=msgget(k,IPC_CREAT|0666); strcpy(s.a,"Welcome"); msgsnd(k,s.a,50,0); printf("\n Msg sent"); msgrcv(k,&s,50,0,0); printf("\n Msg is:%s\n",s.a); } Output : vinhai@vinhai-desktop$ gcc ms.c vinhai@vinhai-desktop$ ./a.out Msg sent Msg is:Welcome vinhai@vinhai-desktop$

Unix Programs

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Unix Programs

Unix Lab

IPC Using Message Queues

#include<stdio.h>#include<sys/ipc.h>#include<string.h>main(){

int id;struct{

int type;char a[20];

}s;key_t k=984171582;s.type=1;id=msgget(k,IPC_CREAT|0666);strcpy(s.a,"Welcome");msgsnd(k,s.a,50,0);printf("\n Msg sent");msgrcv(k,&s,50,0,0);printf("\n Msg is:%s\n",s.a);

}

Output :

vinhai@vinhai-desktop$ gcc ms.cvinhai@vinhai-desktop$ ./a.out

Msg sent Msg is:Welcome

vinhai@vinhai-desktop$

Page 2: Unix Programs

IPC Using Pipes

#include<stdio.h>#include<sys/ipc.h>#include<string.h>main(){

int id,fd[2];char s[40];id=fork();if(id>0){

printf("\n Pareent Process \n");strcpy(s,"\n Hello child \n");write(fd[1],s,20);sleep(3);read(fd[0],s,20);printf("\n %s",s);

}else{

printf("\n Child Process \n");strcpy(s,"\n Hello Parent \n");printf("\n Reading Msg:%s",s);write(fd[1],s,20);

}}

Output :

vinhai@vinhai-desktop$ cc ipcp.cvinhai@vinhai-desktop$ ./a.out

Child ProcessReading Message : Hello ParentParent ProcessHello Child

vinhai@vinhai-desktop$

Page 3: Unix Programs

IMPLEMENTATION OF WAIT & SIGNAL USING COUNTING SEMAPHORE

#include<unistd.h>#include<stdio.h>#include<sys/sem.h>#include<sys/types.h>void wait(int s){while(semctl(s,0,GETVAL,0)<=0)semctl(s,0,SETVAL,semctl(s,0,GETVAL,0)-1);}void signal(int s){semctl(s,0,SETVAL,semctl(s,0,GETVAL,0)+1);}int main(){int pid,semid,i;printf("\n Implementetion Using Counting Semaphore\n");semid = semget(0x21,1,0666|IPC_CREAT);pid = fork();wait(semid);printf("\n\t Process,%d,START[",getpid());for(i = 0;i<5;i++){printf((pid ==0?"*" : "#"));fflush(stdout);sleep(1);}printf("]END\n");signal(semid);if(pid>0){wait(0);semctl(semid,0,IPC_RMID,0);}return 0;}

Page 4: Unix Programs

Output :

vinhai@vinhai-desktop$ cc semc.cvinhai@vinhai-desktop$ ./a.out

Implementetion Using Counting Semaphore

Process,1714,START[####]ENDProcess,1714,START[****]END

vinhai@vinhai-desktop$

Page 5: Unix Programs

IMPLEMENTATION OF WAIT & SIGNAL USING BINARY SEMAPHORE

#include<stdio.h>#include<sys/types.h>#include<sys/sem.h>main(){int semid,pid;struct sembuf sop;semid = semget((key_t)35,1,IPC_CREAT|0666);pid=fork();if(pid==0){sleep(2);printf("Child Before Semop \n");sop.sem_num = 0;sop.sem_op = 0;sop.sem_flg = 0;semop(semid,&sop,1);printf("Child Over \n");} else {printf("Before 1 Semaphore \n");semctl(semid,0,SETVAL,1);Printf("PARENT SLEEP\n");sleep(5);printf("Parent Before 2 Semaphore \n");semctl(semid,0,SETVAL,0);Printf("PARENT Over \n");}}Output :

vinhai@vinhai-desktop$ cc semb.cvinhai@vinhai-desktop$ ./a.out

Before 1 SemaphorePARENT SLEEPChild Before SemopParent Before 2 SemaphorePARENT OverChild Overvinhai@vinhai-desktop$

Page 6: Unix Programs

AUTOMIC COUNTER UPDATE PROBLEM

#include<stdio.h>#include<unistd.h>#include<sys/shm.h>#include<sys/sem.h>#include<sys/ipc.h>#include<sys/types.h>#define key 204main(){ int balance = 500; static struct sembuf unlock[1] = {0,-1,IPC_NOWAIT}; static struct sembuf lock[2] = {0,0,0,0,1,0}; int semid = semget(key,1,IPC_CREAT|0666); int shmid,pid,deposit,withdraw ; void *shmptr; struct shmid_ds myshmid_ds; shmid = shmget(IPC_PRIVATE,1,0666|IPC_CREAT); if(semid<1) { printf(" Semaphore not created "); } pid = fork(); if(pid != 0) { shmptr = shmat(shmid,0,SHM_R); sleep(5); semop(semid,&lock[0],2); balance = *(int *)shmptr; printf("\n Enter the With Draw Ammount : Rs. "); scanf("%d",&withdraw); balance = balance-withdraw; printf("\n Balance After the With Draw Ammount : Rs. %d",balance);

Page 7: Unix Programs

semop(semid,&unlock[0],1); }else { shmptr = shmat(shmid,0,SHM_W); semop(semid,&lock[0],2); printf("\n Enter the Deposit Ammount : Rs. "); scanf("%d",&deposit); balance = balance + deposit; *(int *)shmptr = balance; printf("\n Balance After Deposit : Rs. %d \n",balance); semop(semid,&unlock[0],1); } shmctl(shmid,IPC_RMID,&myshmid_ds); semctl(semid,0,IPC_RMID,0);}

Output :

vinhai@vinhai-desktop$ cc atomic.cvinhai@vinhai-desktop$ ./a.out

Enter the Deposit Ammount : Rs. 1000

Balance After Deposit : Rs. 1500 Enter the With Draw Ammount : Rs. 500 Balance After the With Draw Ammount : Rs. 1000

vinhai@vinhai-desktop$

Page 8: Unix Programs

Counting Semaphores At The User Level Using Binary Semaphores

#include<stdio.h>#include<unistd.h>#include<sys/sem.h>#include<sys/ipc.h>#include<sys/types.h>#define SEM1 0#define SEM2 1#define SEM3 2int count = 3,semid;struct sembuf wait1 = {SEM1,-1,SEM_UNDO};struct sembuf signal1 = {SEM1,1,IPC_NOWAIT};struct sembuf wait2 = {SEM2,-1,SEM_UNDO};struct sembuf signal2 = {SEM2,1,IPC_NOWAIT};struct sembuf wait3 = {SEM3,-1,SEM_UNDO};struct sembuf signal3 = {SEM3,1,IPC_NOWAIT};main(){int pid;union semun{int val;}mysemum;void countwait();void countsignal();semid = semget(IPC_PRIVATE,3,0666|IPC_CREAT);mysemun.val = 1;semctl(semid,SEM1,SETVAL,mysemun);semctl(semid,SEM3,SETVAL,mysemun);mysemun.val = 0;semctl(semid,SEM2,SETVAL,mysemun);pid = fork();if(pid != 0){countwait();countwait();countwait();countsignal();countwait();countsignal();

Page 9: Unix Programs

countsignal();countsignal();countwait();countsignal();} }void countwait(){semop(semid,&wait3,1);semop(semid,&wait1,1);count = count -1;printf("\n Counting Semaphore After Wait is : %d",count);if(count<0){printf("\n Counting Semaphore less than zero ");printf("\n sSem1 : Signal1");semop(semid,&signal1,1);printf("\n sSem2 : wait");}else {semop(semid,&signal1,1);semop(semid,&signal1,1);} }void countsignal(){semop(semid,&wait1,1);count = count +1;printf("\n Value of Counting Semaphore After Signal is : %d",count);if(count<=0){semop(semid,&signal2,1);printf("\n Semaphore Count less than zero signaling sem1 &sem3");}semop(semid,&signal1,1);semop(semid,&signal1,1);}

Page 10: Unix Programs

Output :

vinhai@vinhai-desktop$ cc csem.cvinhai@vinhai-desktop$ ./a.out

Counting Semaphore After Wait is : 2 Counting Semaphore After Wait is : 1 Counting Semaphore After Wait is : 0 Value of Counting Semaphore After Signal is : 1 Value of Counting Semaphore After Signal is : 1 Value of Counting Semaphore After Signal is : 2 Value of Counting Semaphore After Signal is : 3 Counting Semaphore After Wait is : 2 Value of Counting Semaphore After Signal is : 3

vinhai@vinhai-desktop$

Page 11: Unix Programs

SIGNALING PROCESS

#include<stdio.h>#include<signal.h>void s(){

printf("\n This is my signal");}main(){

printf("\n Before signal");signal(SIGALRM,s);alarm(2);pause();printf("\n After Signal \n ");

}

OUTPUT :

vinhai@vinhai-desktop$ cc si.cvinhai@vinhai-desktop$ ./a.out

Before signal This is my signal After Signal

vinhai@vinhai-desktop$

Page 12: Unix Programs

DEADLOCK DETECTION

#include<stdio.h>#include<stdlib.h>main(){

int i,n,j,al[10],re[10];printf("enter the no process : ");scanf("%d",&n);for(i=1;i<=n;i++){

printf("enter the resourse allocated and need for process : %d ",i," "); scanf("%d%d",&re[i],&al[i]);

} for(i=1;i<=n;i++)

{ for(i=j;j<=n;j++){ if(re[i]==al[j])

{ printf("p%d is deadlock because r%d allocated for : p%d",i,re[i],j); exit(0);}}

} }

OUTPUT :

vinhai@vinhai-desktop$ cc deadlock.cvinhai@vinhai-desktop$ ./a.out

enter the no process : 2enter the resourse allocated and need for process : 1 12enter the resourse allocated and need for process : 2 21Segmentation fault

vinhai@vinhai-desktop$

Page 13: Unix Programs

PROCESS SCHEDULING (FCFS)

#include<stdio.h>main(){

int i,n,t=0,s=0,b[10],t1;printf("\n enter the no of values : ");scanf("%d",&n);printf("\n enter the times : ");for(i=0;i<n;i++)scanf("%d",&b[i]);printf("\n\tprocess\t\ttime\t\tstart\t\tend\t\twait");for(i=0;i<n;i++){

s=s+b[i];printf("\n\t%d\t\t%d\t\t%d\t\t%d\t\t%d",i+1,b[i],t,s,t);t1=t;t=s;

}printf("\n avg time%d\n",s/n);printf("\b avg waiting time %d \n",t1/n);

}

OUTPUT :

vinhai@vinhai-desktop$ cc fcfs.cvinhai@vinhai-desktop$ ./a.out

enter the no of values : 2 enter the times : 13

process time start end wait

1 1 0 1 0

2 3 1 4 1

avg time 2 avg waiting time 0

vinhai@vinhai-desktop$

Page 14: Unix Programs

PROCESS SCHEDULING (LEAST FREQUENTLY USED)

#include<stdio.h>main(){

int i,j,n,t,a[10],s=0,e=0;printf("\n Enter the n value : ");scanf("%d",&n);printf("\n Enter the s value : ");for(i=0;i<n;i++)scanf("%d",&a[i]);for(i=0;i<n;i++){

for(j=i+1;j<n;j++){

if(a[i]>a[j]){

t=a[i];a[i]=a[j];a[j]=t;

} } }printf("\n Process Time Start End Wait ");for(i=0;i<n;i++){

s=s+a[i];printf("\n %d\t%d\t%d\t%d\t%d \n ",i+1,a[i],e,s,e);e=s;

} }

OUTPUT : vinhai@vinhai-desktop$ gcc i.cvinhai@vinhai-desktop$ ./a.out Enter the n value3 Enter the s value234 Process Time Start End Wait 1 1 0 1 0 2 2 1 3 1 3 3 3 6 3vinhai@vinhai-desktop$

Page 15: Unix Programs

PROCESS SCHEDULING (ROUND ROBIN)

#include<stdio.h>main(){

int i,a[10],n,ts,t=0;printf("\n Enter the Process & Time-Slice : ");scanf("%d %d",&n,&ts);printf("\n Enter the Time ");for(i=0;i<n;i++)scanf("%d",&a[i]);printf("\n Process Time : ");

st:t=0;for(i=0;i<n;i++){

if(a[i]>=ts){

a[i]=a[i]-ts;printf("\n %d %d \n",i+1,ts);t++;

} else if(a[i]!=0) {printf("\n %d %d \n ",i+1,a[i]);a[i]=0;

} }if(t>0)

goto st;}

OUTPUT :

vinhai@vinhai-desktop$ cc rr.cvinhai@vinhai-desktop$ ./a.out Enter the Process & Time-Slice : 22 Enter the Time 31 Process Time : 1 2 2 1 1 1 vinhai@vinhai-desktop$

Page 16: Unix Programs

PRODUCER CONSUMER PROBLEM WITH LIMITED BUFFERS

#include<stdio.h>#include<unistd.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>#include<sys/shm.h>#define NELEN 4#define SEMPTY 0#define SFULL 1main(){

int pid,semid,shmid,status,i;char elem;union semun{ int val;}mysemnum;struct sembuf waitempty={SEMPTY,-1,SEM_UNDO};struct sembuf signalempty={SEMPTY,1,IPC_NOWAIT};struct sembuf waitfull={SFULL,-1,SEM_UNDO};struct sembuf signalfull={SFULL,1,IPC_NOWAIT};struct shmid_ds myshmid_ds;void *shmptr;semid = semget(IPC_CREAT|0666,IPC_PRIVATE<2);if(semid<0){ printf("Semop Creation Error \n");}mysemnum.val=NELEN;semctl(semid,SEMPTY,SETVAL,mysemnum);mysemnum.val=0;semctl(semid,SFULL,SETVAL,mysemnum);shmid=shmgat(IPC_PRIVATE,NELEN,0666|IPC_CREAT);

Page 17: Unix Programs

if(shmid<0){ printf("Shared Memory Error\n");}pid=fork();if(pid<0){ printf("Proccess Creation Error\n");}if(pid==0){ shmptr=shmat(shmid,0,SHM_R); for(i=0;i<4;i++) { semop(semid,&waitfull,1); elem=*((char *)shmptr+(i%NELEN)); printf("Consumed Element : %c \n",elem); semop(semid,&signalempty,1); sleep(1);} } else { shmptr=shmat(shmid,0,SHM_W); for(i=0;i<4;i++) { semop(semid,&waitempty,1); elem='a'+i; printf("Consumed Element : %c \n",elem); *((char *)shmptr+(i%NELEN))=elem; semop(semid,&signalfull,1); sleep(1);} }wait(& status);shmctl(shmid,IPC_RMID,&myshmid_ds);semctl(semid,SEMPTY,IPC_RMID,&mysemnum);}

Page 18: Unix Programs

OUTPUT :

vinhai@vinhai-desktop$ cc prod.cvinhai@vinhai-desktop$ ./a.out

Prodced Element : aConsumed Element : aProdced Element : aConsumed Element : aProdced Element : aConsumed Element : aProdced Element : aConsumed Element : aProdced Element : aConsumed Element : a

vinhai@vinhai-desktop$

Page 19: Unix Programs

DINING PHILOSOPHER PROBLEM

#include<stdio.h>#include<stdlib.h>#include<unistd.h>#define N 3#define EATING 1#define THINKING 1#define left(p) (p)#define right(p) ((P)+1%N)typedef int *Semaphore;Semaphore spoon[N];void philosopher(int me);void pick_up(int me);void pick_down(int me);void signal(Semaphore s)int main(){

system("clear");int i,n;for(i=0;i<N;i++){ spoon[i]=make_sema(); signal(spoon[i]);}for(i=0;i<n;i++){ if(fork()==0)

break; philosopher(i);}return 0;

}

void philosopher(int me){

char *s;int i;for(i=1;i<2;i++){

pick_up(me); printf("Philosopher %d eating for th time \n",me,i);

Page 20: Unix Programs

sleep(EATING); pick_down(me); printf("Philosopher %d eating for th time \n",me,i); sleep(THINKING);}

}

void pick_up(int me){ if(me==0)

{ wait(spoon[right(me)]); printf("Philosopher %d picks up right spoon \n",me); sleep(1); wait(spoon[left(me)]); printf("Philosopher %d picks up right spoon \n",me);}else

{ wait(spoon[left(me)]); printf("Philosopher %d picks up right spoon \n",me);}

}

void pick_down(int me){

signal(spoon[left(me)]);signal(spoon[right(me)]);

}

void signal(Semaphore s){

if(write(s[1],"x",1)<=0){ printf("Error ! Write() failed, check semaphore creation \n"); exit(1);}

}

Page 21: Unix Programs

void wait(Semaphore s){

int junk;if(read(s[0],&junk,1)<=0){ printf("Error ! Write() failed, check semaphore creation \n"); exit(1);}

}

Semaphore make_sema(void){

int *sema;sema = (int *)calloc(2,siyeof(int));pipe(sema);return sema;

}

OUTPUT :

vinhai@vinhai-desktop$ cc phil.cvinhai@vinhai-desktop$ ./a.out

Philosopher 0 picks up right spoonPhilosopher 0 picks up left spoonPhilosopher 0 eating for the 1 timePhilosopher 0 thnking for the 1 time

Philosopher 1 picks up right spoonPhilosopher 1 picks up left spoonPhilosopher 1 eating for the 1 timePhilosopher 1 thnking for the 1 time

Philosopher 2 picks up right spoonPhilosopher 2 picks up left spoonPhilosopher 2 eating for the 1 timePhilosopher 2 thnking for the 1 time

vinhai@vinhai-desktop$

Page 22: Unix Programs

Reader-Writer Problem

#include<stdio.h>#include<sys/ipc.h>#include<sys/types.h>#include<sys/sem.h>#include<sys/msg.h>#define N 10void writer();void reader();void quit(int,int);int semid,msgid;main(){

int num,ch;semid=semget((key_t)0x43,1,IPC_CREAT|0666);printf("Semaphore value is %d \n",semid);msgid=msgget((key_t)43,IPC_CREAT|0666);for(;;){ printf("1. WRITER \n"); printf("2. READER \n"); printf("3. EXit \n"); printf("Enter the choice \n"); scanf("%d",&ch); switch(ch) {

case1:writer();break;

case2:reader();break;

case3:quit(semid,msgid);break;default:

printf("Wrong Choice \n"); }

}}

Page 23: Unix Programs

void writer(){

char item[20];int num,i;struct sembuf sop;printf("Writer is in critical section \n");printf("Enter the num of items to be written\n");scanf("%d",&num);if(num<(N-semctl(semid,0,GETVAL,0))){ sop.sem_num-0; sop.sem_op=num;

sop.sem_flg=0; semop(semid,&sop,1); for(i=0;i<num;i++) {

printf("Print message[%d]\n",i+1);scanf("%s",item);msgsnd(msgid,&item,strlen(item),IPC_CREAT);

}}else{ printf("Cannot write %d message \n",num); }}

void reader(){

char item[20];int num,i,n1;struct sembuf sop;printf("Writer is in critical section \n");printf("Enter the num of items to be written \n");scanf("%d",&num);if(semctl(semid,0,GETVAL,0))>0){ n1=semctl(semid,0,GETVAL,0); printf("The queue has %d items \n",n1);

Page 24: Unix Programs

if(num<=n1) {

sop.sem_num=0; sop.sem_op=num; sop.sem_flg=0; semop(semid,&sop,1); for(i=0;i<num;i++) {

msgrcv(msgid,&item,50,0,IPC_CREAT);printf("The item received iw %s \n",item);

} }}else {

printf("There is no message \n");}

void quit(int semid,int msgid){

semctl(semid,0,IPC_RMID,0);msgctl(msgid,IPC_RMID,0);exit(0);

}

Page 25: Unix Programs

Output :

vinhai@vinhai-desktop$ gcc rw.cvinhai@vinhai-desktop$ ./a.outSemaphore value is 8192031. WRITER2. READER3. EXITEnter the choice1Writer is in critical sectionEnter the num of items to written2Enter Message[1] : haiEnter Message[2] : cst1. WRITER2. READER3. EXITEnter the choice2reader is in critical sectionEnter the num of items to written1The queue has 2 itemsThe item recived is haiThe item recived is cst1. WRITER2. READER3. EXITEnter the choice3vinhai@vinhai-desktop$

Page 26: Unix Programs

Two Process Mutual Exclusion

#include<stdio.h>#include<unistd.h>#include<sys/ipc.h>#include<sys/msg.h>#include<sys/types.h>#include<string.h>#define key 200main(0{int msgqid,pid;struct{int mtype;char mtext[30];}buf;msgqid=msgget(key,IPC_CREAT|0666);if(msgqid==-1){printf("Error in the message queue creation");exit(0);}pid=fork();if(pid==-1){printf("Error in process creation");exit(1);}if(pid==0){strcpy(buf.mtext,"Two process mutual exclusion");buf.mtype=2;msgsnd(msgqid,&buf,sizeof(buf.mtext),IPC_NOWAIT);strcpy(buf.mtext,"Unix program");buf.mtype=1;msgsnd(msgqid,&buf,sizeof(buf,mtext),IPC_NOWAIT);}

Page 27: Unix Programs

else{msgrcv(msgqid,&buf,sizeof(buf.mtext),IPC_NOWAIT);printf("\n Message is:%s",buf.mtext);msgrcv(msgqid,&buf,sizeof(buf,mtext),IPC_NOWAIT);printf("\n Message is:%s",buf.mtext);}}

Output :

vinhai@vinhai-desktop$ gcc tpme.cvinhai@vinhai-desktop$ ./a.out

Msg is:Two Process Mutual Exclusion Msg is:Unix Program

vinhai@vinhai-desktop$