Question

Please, please help!(Programming Assignment: fork gymnastics) Write a C/C++ program (call it string-invert) that takes a string argument from the command line and outputs the string in reversed order You have two constraints: Constraint 1: Each process can output at most one character. If you want to output more than a single character, you must fork off one or more processes in order to do that, and each of the forked processes in turn outputs a single character t 2: Each process can fork-off at most one other process. After the call to program string invert with the command line argument, the output should appear, and no more processes should be running, in addition to the shell. Test your program on any UNIX/LINUX machine, and turn in the source code as part of the written assignment. (The source code should be at most a few lines long.)

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

Program Screenshot:

# include<stdio. h> # include<stdlib . h> # include<sys/wait.h> # include«unista. h> # include<string.h> #define SIZE 200 //definition of the main function. //takes a string argument from the command //line and outputs the string in reversed order int main (int argc, char argv]) //declare a string variable char str [SIZE]; //declare a pointer char *ptr; //declare pid t pid t pid int temp=0; //copy the command line input //int the string str. strcpy (str, (char*) argv [1]); //print the input string printf (Input string: %s, str); fflush (stdout); ptr str; printf (Reverse string: ); for (int i-strlen (str) 1;i 0i--) //each process can output at most one character. if (pidfork() - 0) //outputs a single character printf(%c, ptr [1]); break; //Each process can fork-off at most one other process wait (&temp if (pid0) printf ( );

Sample output 1:

Input string: Creativity takes courage. Reverse string: .egaruoc sekat ytivitaerC

Sample output 2:

Input string: Good morning! Reverse string: !gninrom doodG

Code to copy:

#include<stdio.h>

#include<stdlib.h>

#include<sys/wait.h>

#include<unistd.h>

#include<string.h>

#define SIZE 200

//definition of the main function.

//takes a string argument from the command

//line and outputs the string in reversed order

int main(int argc, char *argv[])

{

     //declare a string variable

     char str[SIZE];

     //declare a pointer

     char *ptr;

     //declare pid_t

     pid_t pid;

     int temp = 0;

     //copy the command line input

     //int the string str.

     strcpy(str, (char*)argv[1]);

     //print the input string

     printf("Input string: %s", str);

     fflush(stdout);

     ptr = str;

     printf("Reverse string: ");

     for (int i = strlen(str) - 1;i >= 0;i--)

     {

           //each process can output at most one character.

           if (pid = fork() == 0)

           {

                //outputs a single character

                printf("%c", ptr[i]);

                break;

           }

           //Each process can fork-off at most one other process

           wait(&temp);

     }

     if (pid == 0)

           printf(" ");

}

Add a comment
Know the answer?
Add Answer to:
Please, please help! (Programming Assignment: fork gymnastics) Write a C/C++ program (call it string-invert) that takes...
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
  • Write a C or C++ program A8p1.c(pp) that accepts one command line string parameter. Call the...

    Write a C or C++ program A8p1.c(pp) that accepts one command line string parameter. Call the fork function to produce two processes. In the child process print out the lower case version of the string. In the parent process print out the reversed upper case version of the string (e.g. child: “abc”; parent: ”CBA”). You may call the toupper and tolower functions in the header <ctype.h> if you wish. Specify in the output whether the parent or child process is...

  • Assignment: Using the Fork System Call The Collatz conjecture concerns what happens when we take any...

    Assignment: Using the Fork System Call The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorthm: n={n / 2 , if n is even 3 * n + 1 , if n is odd The conjecture states that when this algorithm is continually applied, all positive integers will eventually reach 1. For example, if n = 35, the sequence is:  35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2,...

  • I need help with a java program Write a program Enigma that takes a single String...

    I need help with a java program Write a program Enigma that takes a single String as a command line argument. Enigma should read the file specified by the String argument, add 5 to each byte, and leave the altered data values in a file whose name is the command line argument. Note that this "updating in place" is the most difficult part of this lab: java Enigma sophie.dat should read file sophie.dat, and upon completion, leave the modified data...

  • Write in C. Simple Program (beginner) Assignment: Write a program Character Pointers and Functions. (like program...

    Write in C. Simple Program (beginner) Assignment: Write a program Character Pointers and Functions. (like program #5-5). Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count. <END>

  • write a function which takes a string argument and a character argument. It should return a...

    write a function which takes a string argument and a character argument. It should return a truth value (int 0 or 1), 0 if the string does not contain the character, and 1 if the string does contain the character. Do not use a built-in library for this. Again, call this function and its variables whatever you deem appropriate. The main function of the program should accept a single character followed by Enter. Then, it will read lines until the...

  • C program To develop a C program to implement a process system call. PROBLEM You are...

    C program To develop a C program to implement a process system call. PROBLEM You are to use the Ubuntu operating system to write a C program that creates a process to determine the identification of the current user of your computer. I mean if joe is the login of the current user of your computer your solution should return joe. Your solution must also provide the pid of both the parent and the child processes. You may use the...

  • I need help with this assignment in C++, please! *** The instructions and programming style detai...

    I need help with this assignment in C++, please! *** The instructions and programming style details are crucial for this assignment! Goal: Your assignment is to write a C+ program to read in a list of phone call records from a file, and output them in a more user-friendly format to the standard output (cout). In so doing, you will practice using the ifstream class, I'O manipulators, and the string class. File format: Here is an example of a file...

  • C ++ Implement cat command The purpose of this assignment is to provide practice using the...

    C ++ Implement cat command The purpose of this assignment is to provide practice using the system calls we discussed for working with files on a UNIX system. You will be writing a basic implementation of the cat command using C++. Description As you should recall, the cat command takes a list of files as command line arguments. It then opens each file in turn, writing each file’s entire contents to standard output in the order they were supplied. You...

  • C homework help pipes

    Round 2: pipeline.cThis program takes the same input as sequence.c, but executes the commands as a pipeline, where the output of each command is piped to the input of the next command in line. The input of the first command, and the output of the final command, should not be altered. For example, if the file "cmdpipe" contains the linesls -s1sort -ntail -n 5then running./pipeline < cmdpipeshould output the 5 largest files in the current directory, in order of size.Suggested approach: set...

  • Using Unix processes Submit a README file that lists the files you have submitted along with...

    Using Unix processes Submit a README file that lists the files you have submitted along with a one sentence explanation. Call it Prj1README. MakeCopy.c : Write a C program that makes a new copy of an existing file using system calls for file manipulation. The names of the two files and copy block sizes are to be specified as command line arguments. Open the source file in read only mode and destination file in read/write mode. ForkCopy.c : Write a...

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