C Homework Help

Discussion in 'OT Technology' started by mrburner, Jan 31, 2004.

  1. mrburner

    mrburner Ron Paul 2008

    Joined:
    May 9, 2001
    Messages:
    7,448
    Likes Received:
    0
    Location:
    Phoenix
    i have a C program that we just got assigned...i am basically just very confused...i've done java and c++ (and as some of you know, i'm not great at c++, but i am alright)...i've never coded in C but i'm assuming it is similar

    anyways, here is my assignment...i am basically just having problems getting started...if anyone could give me any tips that i might be able to use for this assignment, it would be much appreciated...thanks

    [font=&quot]CS 353 Operating Systems[/font]
    [font=&quot]Programs 1 - 3[/font]




    Due: February 3, beginning of class



    Program 1

    [font=&quot]Following the example from class, write a C program that does a [/font][font=&quot]fork()[/font][font=&quot]. There should be a function [/font][font=&quot]childsPlay()[/font][font=&quot] that is executed by the child process and a function [/font][font=&quot]parentsWork()[/font][font=&quot] that is executed by the parent process. The parent and child should have access to a globally defined array of integers that has been populated with random numbers in the range 100 to 1000. [/font]




    • [font=&quot]Each process should print out its process id once. For example:[/font]
    [font=&quot] Process[2840]: Child in execution ...[/font]




    • [font=&quot]Then each process should walk through the global array, printing the values, one per line using the format shown below. Make sure you identify the array position and which process is doing the writing. For example:[/font]

    [font=&quot]Parent: intarray[0] = 175[/font]

    [font=&quot]Parent: intarray[1] = 204[/font]

    [font=&quot]Child: intarray[0] = 175[/font]

    [font=&quot]Child: intarray[1] = 204[/font]




    • [font=&quot]Each process should announce when it is terminating. For example,[/font]
    [font=&quot]Process[2839]: Parent terminating ...[/font]



    [font=&quot] Do the following and answer the questions.[/font]


    • [font=&quot]Make the size of the array 10 and do a [/font][font=&quot]sleep(1)[/font][font=&quot] after each value is printed out. Describe the pattern of the output. Why do you think you see this pattern?[/font]
    • [font=&quot]Remove the [/font][font=&quot]sleep()[/font][font=&quot] call from either the child or the parent and rerun the program. Describe the pattern of the output. Why do you think you see this pattern?[/font]
    • [font=&quot]Now remove the other [/font][font=&quot]sleep()[/font][font=&quot] call and bump the array size to 1000. Rerun the program (I redirected the output to a file so I could look at it more easily. Also, you might want to do this a few times and see if the results are different.) Describe the pattern of the output. Why do you think you see this pattern?[/font]



    Hints

    [font=&quot]Do a man on rand, srand, fork, sleep and time.[/font]





    Program 2

    [font=&quot]Write a program that does a [/font][font=&quot]fork()[/font][font=&quot] and has the child process to an [/font][font=&quot]execve()[/font][font=&quot] to execute a program called [/font][font=&quot]fileCopy[/font][font=&quot]. Program [/font][font=&quot]fileCopy[/font][font=&quot] (which you must also write) copies the contents of one file to another. The name of the input and output files are to be supplied on the command line. So the following copies [/font][font=&quot]blue.txt[/font][font=&quot] to [/font][font=&quot]ginger.txt[/font]



    [font=&quot][blah:Courses/OS/Code] blah% prog2 blue.txt ginger.txt[/font]



    [font=&quot]Note that it is [/font][font=&quot]prog2[/font][font=&quot] that does the fork and exec to run [/font][font=&quot]fileCopy[/font][font=&quot]. The parent process should wait until the child is done to exit.[/font]



    [font=&quot]Hint: You should get [/font][font=&quot]fileCopy[/font][font=&quot] working, then write the parent that does the fork/execve sequence. [/font]







    Program 3

    [font=&quot]Write a C program that takes as its command line arguments the name of an executable and the arguments to be sent to that executable and does a fork/exec sequence to execute the program given on the command line. For example, [/font]



    [font=&quot][blah:Courses/OS/Code] blah% prog3 fileCopy blue.txt ginger.txt[/font]



    [font=&quot]will execute [/font][font=&quot]fileCopy[/font][font=&quot] to copy [/font][font=&quot]blue.txt[/font][font=&quot] to [/font][font=&quot]ginger.txt[/font][font=&quot]. The parent process should wait until the child is done to exit.[/font]



    [font=&quot]Hint: if you have program 2, this one is a gift. [/font]J
     
  2. ChosenGSR

    ChosenGSR Mama always said you'd be the chosen one

    Joined:
    Oct 24, 2001
    Messages:
    51,116
    Likes Received:
    243
    Location:
    HoCo, MD
    Well, one could probably write a book as a reply... Here is something to think about. When a process is forked from the main(parent) process a PID is returned. The child process gets and EXACT copy of the parent code/vars. The child process begins execution from the point where fork left off. From that point on, you have simple brenching structure. if/else on the PID. If the PID is 0, then you are within the child process. If the PID is not 0, you are in the main. Of course you should also check for -1. in case and error was returned.

    Everything else should be simple programming that you already know. Ask away if you have any more questions.
     

Share This Page