Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Assignment 4 -Using Unix systems calls fork() wait() read() and write() System Programming

Using Unix systems calls, fork(), wait(), read() and write(), write a C program for integerbasic
arithmetic to perform the followings:
• writes the message “This program makes simple arithmetics”,
• gets in an infinite loop then
1. writes the message “Enter an arithmetic statement, e.g., 34 + 132 > “,
2. reads the whole input line,
3. forks and
– the parent writes the message “Created a child to make your operation, waiting”
then calls wait() to wait for its child.
– the child process calls the function childFunction(char *) and never returns.
4. the child, through childFunction(char *line),
– writes the message “I am a child working for my parent”
– uses sscanf() to convert the input line into an integer, a character and an integer,
respectively.
– in case of wrong statement, the child calls exit(50)
– in case of division by zero, the child calls exit(100)
– in case of a wrong op the child calls exit(200)
– otherwise, it performs the appropriate arithmetic operation,
– uses sprint() to create an output buffer consisting of n1 op n2 = result,
– writes the output bufferto the screen
– calls exit(0)
5. once the child terminates, the parent checks the returned status value and if it is
50, 100 or 200, writes “Wrong statement”, “Division by zero” or “Wrong operator”,
respectively.
6. the parent goes back to 1.

Answer:

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

int childFunction(char *string);

char input;
char *string = &input;
char str[100];
int status;
int pid;


int main (void)
{
    printf("This program makes simple arithmetics\n");
    
    while(1)    
    {
        write(1,"Enter an arithmetic statement\n",31); 
        input = scanf ("%[^\n]%*c", str);   
        
        pid = fork();  
        
        if (pid == -1)  
        {
            perror("Impossible to fork\n");
            exit(0);
        }
        
        else if (pid == 0)    
            childFunction(str);
        
        else
        {
            if (pid > 0)  
            {
                write(1,"\nCreated a child to make your operation, waiting\n",49); 
                wait(&status);  
                
                if (WEXITSTATUS(status) == 50)  
                    printf("Wrong Statement\n\n");
                
                else if (WEXITSTATUS(status) == 100)    
                    printf("Division by zero\n\n");
                
                else
                    if (WEXITSTATUS(status) == 200) 
                        printf("Wrong operator\n\n");
            }
        }
        
    }
    
    return (0);
}



int childFunction(char *string)
{
    int n1, n2;
    char op;
    float div = n1/n2;
    
    write(1,"I am a child working for my parent\n",36); 
    sscanf(string, "%d %c %d", &n1, &op, &n2);  
    
    sleep(1);
    
    if ((sscanf(string,"%d %c %d", &n1, &op, &n2)) != 3)    
        exit(50);
    
    if (op == '/' && n2 == 0)   
        exit(100);
    
    switch (op) 
    {
        case '+':
            printf("%d %c %d = %d\n\n", n1, op, n2, n1+n2);break;
            
        case '-':
            printf("\n%d %c %d = %d\n\n", n1, op, n2, n1-n2);break;
            
        case '/':
            printf("\n%d %c %d = %f\n\n", n1, op, n2, div);break;
            
        case '*':
            printf("\n%d %c %d = %d\n\n", n1, op, n2, n1*n2);break;
            
        default:
            exit(200);break;
    }
    exit(0);
    
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!