User Tools

Site Tools


user:bkenne11:portfolio:fork

Project: YOUR PROJECT NAME HERE

A project for Brandon Kennedy by Brandon Kennedy during the 2011 Fall Semester.

This project was begun on 11/07 and is anticipated to take 2 days.

Objectives

The purpose of this project is to briefly explore the functionality of the fork() function. I hope to walk away with the ability to utilize the fork() function in a program where concurrency is applicable.

Prerequisites

In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

  • C Pocket Reference
  • the C programming guide
  • Class documentation on the fork() function

Background

I am pursuing an understanding of the fork() function and its uses. I will write a program that will fork itself once so that there are 2 instances of the same program running. It will then save the PID's of the two instances and have each instance open, edit, and close the same file.

Attributes

State and justify the attributes you'd like to receive upon successful approval and completion of this project.

  • Processes: This function will use fork()
  • Command-line Arguments: This function will use argc and argv
  • file I/0: this program will make and access regular files.

Code

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main(int argc, char **argv)
{
        pid_t Par, Chi;
        char input;
        FILE *fPtr;
        FILE *fPtr2;
        int flag = 0;
        int i;
        Par = getpid();
        printf("This program, %s, has a PID of %d\n", argv[0], Par);
        Chi = fork();
 
        if(getpid() == Par)
        {
                fPtr = fopen("parent", "w");
                fprintf(fPtr, "This file contains the process ID of the parent and child in the myfork.c program.\n\n");
                fprintf(fPtr, "The PID of the parent is %i\n\n",Par);
                fclose(fPtr);
                flag = 1;
        }
        else
        {
                while(flag == 0)
                {
                        sleep(1);
                }                  //Make sure the parent finishes first
                fPtr = fopen("parent", "w");
                fprintf(fPtr2, "The PID of the child is %i\n\n", Chi);
                fclose(fPtr2);
                exit(1);
        }
        return(0);
}

Execution

lab46:~/src/SysProg/fork$ ./myfork
This program, ./myfork, has a PID of 22854

Reflection

FORK IS AWESOME!!! Also, I learned a lot about file access from a c program. I think this project will turn into something much bigger once i start utilizing file access in c programs.

References

In performing this project, the following resources were referenced:

  • C pocket refrence

Generally, state where you got informative and useful information to help you accomplish this project when you originally worked on it (from Google, other wiki documents on the Lab46 wiki, etc.)

user/bkenne11/portfolio/fork.txt · Last modified: 2011/12/15 01:44 by bkenne11