Question

Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this...

Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this problem. You can still read/write from/to a file. You must use ( kill() and pause() ) system calls.  

rewrite code below to do kill and pause system calls

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>

// Function ptototypes
int readX();
void writeX(int);

int main()
{
        int pid;                        // pid: used to keep track of the child process
        int x = 19530;          // x: our value as integer
        int i;                          // iterator     

        // Write x to file
        writeX(x);

        // output inital value to the screen
        printf("x = %d\n", x);

        // loop 5 times
        for( i = 0; i<5; i++)
        {
                // output the current loop iteration
                printf("\nITERATION %d\n", i+1);

                // flush here so that the stdout buffer doesn't
                //              get printed in both processes
                fflush(stdout);

                // fork child process
                if ((pid = fork()) == -1)
                {
                        perror("Error with fork");
                        exit(1);
                }

                // Child waits a second for the parent to go first
                else if (pid ==0)
                {
                        sleep(1);
                        // NOTE: may need to sleep more seconds for busy or slower OSes
                }

                // Both processes read x from file
                x = readX();

                // Parent prints parent, and performs subraction
                if (pid > 0)
                {
                        printf("Parent: ");
                        x -= 5;
                }

                // Child prints child, and performs division
                else if ( pid == 0 )
                {
                        printf("Child: ");
                        x = x/5;
                }

                // Both processes output x value and write x to file...
                printf("x = %d\n", x);
                writeX(x);

                // The child terminates
                if (pid == 0)
                {
                        exit(0);
                }

                // Parent waits for child to terminate before continuing loop
                wait(NULL);
        }

        exit(0);
}

/// Returns the value read from .shareX.dat
int readX()
{
        char xChar[10];         // xChar: our value as a char
        int fd;                         // fd: file descriptor

        // open to read and store x
        if ( (fd = open(".shareX.dat", O_RDONLY )) == -1 )
        {
                perror("Error opening file");
                exit(2);
        }

        // read xChar from the file
        if ( read(fd, xChar, 10) == -1 )
        {
                perror("Error reading file");
                exit(3);
        }

        // close file for reading
        close(fd);

        // convert xChar to int and return
        return  atoi(xChar);
}

/// Writes the writeX value to the file .shareX.dat
void writeX(int writeX)
{
        char xChar[10];         // xChar: our value as a char
        int fd;                         // fd: file descriptor
        int xBytes;                     // how much to write

        // open, clear, and create file if not createdi
        if ( (fd = open(".shareX.dat",
                                        O_CREAT | O_TRUNC | O_WRONLY, 0644 )) == -1 )
        {
                perror("Error opening file");
                exit(4);
        }

        // convert x to char
        xBytes = sprintf(xChar, "%d", writeX);

        // put xChar in file
        if ( write(fd, xChar, xBytes) == -1 )
        {
                perror("Error writing to file");
                exit(5);
        }

        // close the file for writing
        close(fd);

        return;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>

// Function ptototypes
int readX();
void writeX(int);

int main()
{
        int pid;                        // pid: used to keep track of the child process
        int x = 19530;          // x: our value as integer
        int i;                          // iterator   

        // Write x to file
        writeX(x);

        // output inital value to the screen
        printf("x = %d\n", x);

        // loop 5 times
        for( i = 0; i<5; i++)
        {
                // output the current loop iteration
                printf("\nITERATION %d\n", i+1);

                // flush here so that the stdout buffer doesn't
                //              get printed in both processes
                fflush(stdout);

                // fork child process
                if ((pid = fork()) == -1)
                {
                        perror("Error with fork");
                        exit(1);
                }

                // Child waits a second for the parent to go first
                else if (pid ==0)
                {
                        sleep(1);
                        // NOTE: may need to sleep more seconds for busy or slower OSes
                }

                // Both processes read x from file
                x = readX();

                // Parent prints parent, and performs subraction
                if (pid > 0)
                {
                        printf("Parent: ");
                        x -= 5;
                }

                // Child prints child, and performs division
                else if ( pid == 0 )
                {
                        printf("Child: ");
                        x = x/5;
                }

                // Both processes output x value and write x to file...
                printf("x = %d\n", x);
                writeX(x);

                // The child terminates
                if (pid == 0)
                {
                        exit(0);
                }

                // Parent waits for child to terminate before continuing loop
                pause();
        }
        kill(pid,0);
     
}

/// Returns the value read from .shareX.dat
int readX()
{
          int pid = fork();
        char xChar[10];         // xChar: our value as a char
        int fd;                         // fd: file descriptor

        // open to read and store x
        if ( (fd = open(".shareX.dat", O_RDONLY )) == -1 )
        {
                perror("Error opening file");
                kill(pid,2);
        }

        // read xChar from the file
        if ( read(fd, xChar, 10) == -1 )
        {
                perror("Error reading file");
               kill(pid,3);;
        }

        // close file for reading
        close(fd);

        // convert xChar to int and return
        return atoi(xChar);
}

/// Writes the writeX value to the file .shareX.dat
void writeX(int writeX)
{
     int pid = fork();
        char xChar[10];         // xChar: our value as a char
        int fd;                         // fd: file descriptor
        int xBytes;                     // how much to write

        // open, clear, and create file if not createdi
        if ( (fd = open(".shareX.dat", O_CREAT | O_TRUNC | O_WRONLY, 0644 )) == -1 )
        {
                perror("Error opening file");
                kill(pid,4);;
        }

        // convert x to char
        xBytes = sprintf(xChar, "%d", writeX);

        // put xChar in file
        if ( write(fd, xChar, xBytes) == -1 )
        {
                perror("Error writing to file");
                kill(pid,5);
        }

        // close the file for writing
        close(fd);

        return;
}

Add a comment
Know the answer?
Add Answer to:
Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...

    GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() /// chi read x ---> divi ---> write x into file ---> par read x --> sub--> write x into file---> chi read x-->etc {    int pid;           // pid: used to keep track of the child process    int x = 19530;       // x: our value as integer   ...

  • Rewrite the program as a set of two programs that utilize a named pipe “ages” in...

    Rewrite the program as a set of two programs that utilize a named pipe “ages” in the current folder for IPC and implement the same function as discussed above. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> int main(void) {         int     fd[2], nbytes;         pid_t   childpid;         int users = 95;         char mystring[80];         char    readbuffer[80];         char digit1,digit2;         digit1 = (char) users%10;         digit2 = (char ) users/10;         mystring[0] = digit1;         mystring[1] = digit2;...

  • The following code is meant to capture the output of an executable in a file and...

    The following code is meant to capture the output of an executable in a file and then read in and print out the first 100 bytes of that file. Select all of the bugs in the code. Assume that all system calls succeed and that sizeof(char)-1. pid_t pid = fork(); if (pid == 0) { int in-open("file.txt", READONLY) dup2(STDOUT FILENO, in); execl("./something", "something", "hello!"); l else char buffer[100] int in = open("file.txt'. READ-ONLY): read(buffer, in, 100); The parent does not...

  • 2Processes. Use the following assumptions to answer the questions: » All processes run to completion and no system calls will fail printf ( is atomic and calls fflush (stdout) after printing argument...

    2Processes. Use the following assumptions to answer the questions: » All processes run to completion and no system calls will fail printf ( is atomic and calls fflush (stdout) after printing argument(s) but before returning 1 int main) ( int status, pid, pid2 pid -fork O if (pid0) printf ("A") else pid2 fork ) if (pid20) printf ("C"); waitpid (pid, &status, 0) printf("D") 4 10 12 13 return; ) else ( printf("B") 14 15 17 19 return 0; 20 [8...

  • I need help fixing this code in C. It keeps giving me an error saying error:...

    I need help fixing this code in C. It keeps giving me an error saying error: ‘O_RDWD’ undeclared (first use in this function) if ((fd = open(fifo, O_RDWD)) < 0) note: each undeclared identifier is reported only once for each function it appears in. Please fix so it compiles properly. //************************************************************************************************************************************************** #include <fcntl.h> #include <stdio.h> #include <errno.h> #include<stdlib.h> #include <string.h> #include<unistd.h> #include <sys/stat.h> #include <sys/types.h> #define MSGSIZE 63 char *fifo = "fifo"; int main(int argc, char **argv){    int fd;...

  • Using the file descriptor table for a process, answer the following questions. Note: the dup(), pipe()...

    Using the file descriptor table for a process, answer the following questions. Note: the dup(), pipe() , and open() system calls always use the lowest numbered available file descriptor posi-tion. (a) Label which entries in Table 2 refer to stdin, stdout, andstderr 0 1 2 3 4 5 (b) Assume that stdin, stdout, and stderr are all open. Label in Table 3 what the file descriptor table would look like after the following code is executed: int fd[2]; close(1); pipe(fd);...

  • Write a program that opens a file (with the open() system call) and then calls fork)...

    Write a program that opens a file (with the open() system call) and then calls fork) to create a new process. Can both the child and parent access the file descriptor returned by open() ? What happens when they are writing to the file concurrently, i.e., at the same time?

  • Modify the template program below and include the following additional functionalities: - Instead of 3 children...

    Modify the template program below and include the following additional functionalities: - Instead of 3 children processes, create 6 children processes. - 6 children processes are connected to the parent process via 6 pipes, one for each pair, child1-parent, child2-parent, child3-parent, .... - Child keeps writing to the pipe with 1 second apart between consecutive writes (as in child() in p. 167). - Keyboard inputs need to be monitored as in the original code. - Parent keeps reading from the...

  • C programming help! /* Your challenge is to format the following code in a readable manner, anwsering the questions in...

    C programming help! /* Your challenge is to format the following code in a readable manner, anwsering the questions in the comments. * */ #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <time.h> #include <sys/socket.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/uio.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <strings.h> #define BUFFERT 512 #define BACKLOG 1 int create_server_socket (int port); struct sockaddr_in sock_serv,sock_clt; int main(int argc, char** argv){ int sfd, fd; unsigned int length = sizeof(struct sockaddr_in); long int n, m, count...

  • IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two...

    IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two arguments (IP add & port no. of the concurrent Server with thread - conServThread.c). Similarly, modify the Server (conServThread.c) program to accept an argument which is the port number of the server to bind and listen to. Try these two updated programs (server and client) with a port number (e.g., hhmm6) with current time where hh is hours in 24-hour format and mm is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT