Jump to content

Help with System Calls and C

Koxxy

I'll be upfront and say that this is part of a homework assignment but Easter break is on and so I cant get any guidance from anyone there.

 

Perhaps you can point me in the right direction!

 

How do we fix this program so that it prints the string “I Heart RU” as output? More specifically,

can you change (modify, add or remove) only one or two characters in the program source to
achieve the result? (Note that modifying one character gives full score, two characters gives half
of the points).
#include <stdio.h>#include <signal.h> pid_t pid; void handler1(int sig) {    printf("I ");    fflush(stdout); /* Flushes the printed string to stdout */    kill(pid, SIGUSR1);} void handler2(int sig) {    printf("Heart ");    exit(0);} /* TODO write documentation */main() {    signal(SIGUSR1, handler1);    if ((pid = fork()) > 0) {        signal(SIGUSR1, handler2);        kill(getppid(), SIGUSR1);        while(1) {};    }    else {        pid_t p; int status;        if ((p = wait(&status)) > 0) {            printf("RU\n");        }    }}

Now what I think is the trick here is to change the (pid = fork()) > 0 to (pik = fork()) = 0 effectively splitting the Child and the Parents process up sending the Child down the if block and the parent down to the else block. The next road block I think is that the child will get stuck on the infinite loop of while(1) and the parent waits for its child to exit in the wait(&status).

 

Any help would be greatly appreciated!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×