Question

In: Computer Science

You need to write and run C programs (as processes on your Linux machine), and monitor...

You need to write and run C programs (as processes on your Linux machine), and monitor their behavior. Consider the following problem: A program is to be written to print all numbers between 1 and 1000 (inclusive) that are not (evenly) divisible by either 2 or 3. This problem is to be solved using three processes (P0, P1, P2) and two one-integer buffers (B0 and B1) as follows:  P0 is to generate the integers from 1 to 1000, and place them in B0 one at a time. After placing 1000 in the buffer, P0 places the sentinel 0 in the buffer, and terminates.  P1 is to read successive integers from B0. If a value is not divisible by 2, the value is placed in B1. If the value is positive and divisible by 2, it is ignored. If the value is 0, 0 is placed in B1, and P1 terminates.  P2 is to read successive integers from B1. If a value is not divisible by 3, it is printed. If the value is positive and divisible by 3, it is ignored. If the value is 0, P2 terminates. Write a program to implement P0, P1, and P2 as separate processes and B0 and B1 as separate pieces of shared memory {each the size of just one integer}. Use semaphores to coordinate processing. Access to B0 should be independent of access to B1; for example, P0 could be writing into B0 while either P1 was writing into B1 or P2 was reading.

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <sys/types.h>

int main(int argc, char const *argv[])
{
   // Creating the shared buffers and semaphores.
   int b0_shmId, b1_shmId;
   int s0_shmId, s1_shmId, s2_shmId, mutex_shmId;

   b0_shmId = shmget(IPC_PRIVATE, 1 * sizeof(int), 0777 | IPC_CREAT);
   b1_shmId = shmget(IPC_PRIVATE, 1 * sizeof(int), 0777 | IPC_CREAT);
   s0_shmId = shmget(IPC_PRIVATE, 1 * sizeof(sem_t), 0777 | IPC_CREAT);
   s1_shmId = shmget(IPC_PRIVATE, 1 * sizeof(sem_t), 0777 | IPC_CREAT);
   s2_shmId = shmget(IPC_PRIVATE, 1 * sizeof(sem_t), 0777 | IPC_CREAT);
   mutex_shmId = shmget(IPC_PRIVATE, 1 * sizeof(sem_t), 0777 | IPC_CREAT);

   int *b0, *b1;
   sem_t *s0, *s1, *s2, *mutex;

   b0 = (int *) shmat(b0_shmId, 0, 0);
   b1 = (int *) shmat(b1_shmId, 0, 0);
   s0 = (sem_t *) shmat(s0_shmId, 0, 0);
   s1 = (sem_t *) shmat(s1_shmId, 0, 0);
   s2 = (sem_t *) shmat(s2_shmId, 0, 0);
   mutex = (sem_t *) shmat(mutex_shmId, 0, 0);

   // Initialising the semaphores.
   sem_init(s0, 0, 1);
   sem_init(s1, 0, 0);
   sem_init(s2, 0, 0);
   sem_init(mutex, 0, 1);

   // Creating the processes.
   pid_t p0, p1, p2;

   if((p0 = fork()) == 0)
   {
       int i;
       for(i=1; i<=1001; i++)
       {
           sem_wait(s0);
          
           b0[0] = i;

           sem_post(s1);
       }

       exit(1);
   }

   if((p1 = fork()) == 0)
   {
       int x = -1;

       while(x <= 1000)
       {
           sem_wait(s1);

           x = b0[0];

           sem_post(s0);
           if(x % 2 != 0)
           {
               sem_wait(mutex);

               b1[0] = x;

               sem_post(mutex);
               sem_post(s2);
           }
       }
       exit(2);
   }

   if((p2 = fork()) == 0)
   {
       int x = -1;

       while(x <= 1000)
       {
           sem_wait(s2);
           sem_wait(mutex);

           x = b1[0];

           sem_post(mutex);

           if(x % 3 != 0)
           {
               printf("%d\n", x);
           }
       }
       exit(3);
   }


   // Waiting for all three processes to finish.
   while(wait(NULL) != 0)
       ;

   // Detaching and destroying the shared memories.
   shmdt(b0);
   shmdt(b1);
   shmdt(s0);
   shmdt(s1);
   shmdt(s2);
   shmdt(mutex);

   shmctl(b0_shmId, IPC_RMID, 0);
   shmctl(b1_shmId, IPC_RMID, 0);
   shmctl(s0_shmId, IPC_RMID, 0);
   shmctl(s1_shmId, IPC_RMID, 0);
   shmctl(s2_shmId, IPC_RMID, 0);
   shmctl(mutex_shmId, IPC_RMID, 0);


   return 0;
}


Related Solutions

Write a C program to synchronize multiple producer processes and multiple consumer processes using monitor
Write a C program to synchronize multiple producer processes and multiple consumer processes using monitor
2. Given the Instruction sets for Machine-1, Machine-2 and Machine-3, write programs to calculate: P= ((B×C)...
2. Given the Instruction sets for Machine-1, Machine-2 and Machine-3, write programs to calculate: P= ((B×C) + (A +D)) / (E - C × D) and comment on relative efficiency. Machine-1: One address instructions Machine-2: Two address instructions Machine-3: Three address instructions STORE X ; X<― [AC] LOAD X ; AC<― [X] MPY X ; AC<― [AC x X] DIV X ; AC<― [AC / X] ADD X ; AC<― [AC + X] SUB X ; AC<― [AC -X] X...
1.Write complete programs. These programs should be able to compile and run. No psuedocode is allowed....
1.Write complete programs. These programs should be able to compile and run. No psuedocode is allowed. No comments are needed. These programs will be graded for syntax, semantics, and programming style. Write a complete program to display the following output: Plan to be spontaneous tomorrow. 2. Write complete programs. These programs should be able to compile and run. No psuedocode is allowed. No comments are needed. These programs will be graded for syntax, semantics, and programming style. Write a complete...
please write in c using linux or unix Write a program that will simulate non -...
please write in c using linux or unix Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
Please write in C using linux or unix. Write a program that will simulate non -...
Please write in C using linux or unix. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
Introduction Write in C++ at the Linux command line a program that is the same as...
Introduction Write in C++ at the Linux command line a program that is the same as the previous collection app project but now uses a class to store the items and also can save the items to a file that can be read back into the array by the user when the program is re-started. You can use your project 1 submission as a starting point or you can do something new as long as it meets the listed requirements....
In Project 13-1, you configured the DHCP daemon (dhcpd) on your Fedora Linux virtual machine. On...
In Project 13-1, you configured the DHCP daemon (dhcpd) on your Fedora Linux virtual machine. On your Ubuntu Server Linux virtual machine, install and configure udhcpd to provide the same functionality described in Project 13-1, and test your settings by configuring your Fedora Linux virtual machine as a DHCP client that obtains IPv4 configuration from udhcpd. Finally, restore your original configuration.
IN C LANGUAGE: Write a multi-threaded Linux program that synchronizes it's threads to write to a...
IN C LANGUAGE: Write a multi-threaded Linux program that synchronizes it's threads to write to a file without the file becoming corrupted. To do this, your program will create three threads which write strings to the same file. Each thread will randomly write a selection of strings to the file at random intervals. When finished, the file will contain all the strings written correctly to the file. You may use mutexes, semaphores, or a monitor your write on your own....
Please write in C using linux or unix.please include pictures of the terminal output. Write a...
Please write in C using linux or unix.please include pictures of the terminal output. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the...
Linux Directories, File Properties, and the File System in C Understanding Unix/Linux Programming Your version of...
Linux Directories, File Properties, and the File System in C Understanding Unix/Linux Programming Your version of mv command The mv command is more than just a wrapper around the rename system call. Write a version of mv that accepts two argument. The first argument must be the name of a file, and the second argument may be the name of a file or the name of a directory. If the destination is the name of a directory, then mv moves...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT