It's the last set of homework problems, yay. But I'm still as stuck as I was before.
"1. Write a program that creates a pipe and forks a child. The program should accept a single argument and pass it across the pipe to the child. The child should read the pipe and print what it receives. The child should have its own code and not call an exec function. Since the child won't know how many bytes are being sent, it should keep reading until there are no more bytes. The child should call _exit(0) and the parent should wait on the child and print its exit status. Name the program send.c.
2. Write a program that performs an ls -l on the filename given as the program's first argument and pipes the listing to the grep command where grep is looking for directories in the listing. Fork a child process to run the ls -l. Let the parent run grep. Use execvp for both parent and child. Name the program dirs.c.
3. Write a program that prints the numbers from 1 to 25, sleeping one second after each number. If signal SIGUSR1 arrives, it should print the total so far, sleep 3 seconds, and resume printing numbers. Name the program sig.c."
Here's where I stand on each of these:
1. I'm kinda stuck on two points:
a. How do get the number of bytes in the single argument that you're passing into the pipe?
b. How exactly do the exit and wait system calls work in this situation? An example in the textbook declares an integer named status, yet assigns nothing to it. Later in the example, the wait system call refers to the address of the status variable out of nowhere. Is status predefined in a library or something? I apologize if I'm butchering programming terms but here's what I'm seeing:
Code:
#include <stdio.h>
#include <wait.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
int status;
...
(rest of the code; no action is taken on status)
...
wait(&status);
}
2. I think I've got this one. I know that my prof hinted at basically modifying the existing examples that he pulled from the textbook, which I THINK I've got covered here.
3. I haven't started on yet. I'll probably place #1 on hold just to try this one out.