part a:
First of all, you are supposed to write a program in either C or C++ that reads data from a file and copies it to another file. Your program should copy all contents of the file.
part b:
Secondly, write another program to create child processes from your main program recursively using fork( ) and exec( ) commands up to a certain level, for example, fifth level. Then print identification of each child process in each level.
My code:
Part A:Code:
#include<stdio.h>
#include <string.h>
int main(void)
{
// open a text file for reading
FILE *in_file
in_file = fopen("input.txt","r");
// open a text file for writing
FILE *out_file
out_file = fopen("output.txt","w");
if(!f)
{ return 1; }
while(fgets(s,80,in_file) != NULL)
{
//write to file
fprintf(out_file, "%s",s");
fprintf(out_file, "\n");
}
fclose(in_file);
fclose(out_file);
system("pause");
return 0;
}
Part B:Code:
#include <stdio.h>
#include <unistd.h>
int main()
{
/* call the recursive function */
recurse(5);
}
void recurse(int count)
{
Pid_t pid;
pid = fork();
if(pid < 0)
{
/* if the fork failed give error */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0)
{
/* child process and print the ID*/
execlp("/bin/ls", "ls", NULL);
printf("Child Process ID: " + pid);
}
else
{
/* parent process */
/* parent will wait for the child to complete */
wait (NULL);
exit(0);
}
/* if the count is less than one, we're finished */
if(count > 1)
{
exit(0);
}
/* decrement count and call itself (recursion) */
else
{
count = count - 1;
recurse(count - 1)
}
}
Please helpe me?!!?! What am I doing wrong? What am I missing? This guy told me it'd be easy because I "know" Java and it isn't. I honestly think learning Ada was a lot easier than C.
Edited by WingedPanther, 24 March 2008 - 07:51 AM.
add code tags


Sign In
Create Account

Back to top









