This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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);}
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
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$
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
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++){
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++){
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$
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 : ");
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$
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
pick_up(me); printf("Philosopher %d eating for th time \n",me,i);
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);}
#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"); }
}}
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;
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);
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$
#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);}