Write Source Code to Implement Semaphore

To write a C program to implement Semaphore.
Algorithm | Source Programming
1. Start the program.
2. Get the no of jobs from the user.
3. When job1 is processing, job 2 is also starts processing.
4. When job 1 enters critical section, next job starts processing.
5. When job1 comes out of the critical section, the other job enters the critical section.
6. The above 3 steps are performed for various programs.
7. End the program.
Example Source code programming in C Program
#include<stdio.h>
main()
{
int i,a=1,h=2,n;
printf("\n Enter the no of jobs");
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(a==1)
{
printf("processing %d......! \n", i+1);
a++;
}
if(h>1)
{
if(i+2<=n)
{
printf("\n processing %d.....! \n",i+2);
}
printf("\n Process %d Enters Critical section", i+1);
printf("\n Process %d Leaves Critical section", i+1);
}
h+1;
}
}
Example Output Result
"semaphore.c" 25L, 359C written
[staff@linux-router staff]$ cc semaphore.c
[staff@linux-router staff]$ gcc semaphore.c
[staff@linux-router staff]$ ./a.out

Enter the no of jobs
2
processing 1......!
processing 2.....!
Process 1 Enters Critical section
Process 1 Leaves Critical section
Process 2 Enters Critical section
Process 2 Leaves Critical section[staff@linux-router staff]$
How to Write Source Code to Implement Semaphore

Post a Comment

3 Comments