Games: Processes and Inter-Process Communication
Prof. Brian D. Davison
Computer Science & Engineering, Lehigh University
Announcements
- Project 6 revised grades: range 46-100, mean 84
- Thursday we have a featured speaker from GM to speak to all
undergrads (including first year students) about computing in
automobiles
- Friday's quiz is postponed until Monday
- Reminder: project 7 due tomorrow night. Grading sheet is now online.
- Note that Project 8 (last one!) is online if you have finished #7.
- Final exam is scheduled for Wednesday May 4, noon-3pm in Packard 416
- Today: Processes and inter-process communication (IPC)
Processes
- Review: What is a process?
Processes
- Review: What is a process?
- A process is an executing instance of a program.
- The shell is a program designed to help you manage programs and processes.
- User types a.out
- Shell asks kernel for a new process
- Shell asks the kernel to execute the program a.out in that new process
- a.out runs
Running a program
- Q: How can a program run a program?
Running a program
- Q: How can a program run a program?
- A: The program calls execvp(2)
- Usage: execvp(const char *file, char *const argv[])
- Program calls execvp()
- Kernel loads program
- Kernel puts arglist into program
- Kernel calls main(argc, argv)
Example: execdemo.c
- Purpose: run a command
#include <stdio.h>
#include <unistd.h>
main() {
char *args[4] = {"ls", "-l", "/usr/bin", NULL};
printf("before exec\n");
fflush(0);
execvp("ls", args);
printf("after exec\n");
}
- What is the result of running this program?
Getting a new process
- execvp() ran program, but never returned!
- We need to create a new process and have that process run the program.
- Q: How do we get a new process?
Getting a new process
- execvp() ran program, but never returned!
- We need to create a new process and have that process run the program.
- Q: How do we get a new process?
- A: Use fork() to clone an existing process
- fork() takes no arguments
- OS copies memory containing process
- returns -1 on error, 0 for child, and pid of child in parent
- Otherwise, exactly the same program, same place in program
Example: forkdemo3.c
- In forkdemo3.c we see exactly the PID for each process.
- So, using fork(), we can create a new process, which can call execvp() to run the program.
- Let's modify forkdemo3 to do so.
- But what should the parent do?
The Parent wait()s
- We know the child is executing the new process.
- The parent needs to call wait(2) for the child to die.
- Usage: p = wait(&from_child);
- Returns -1 if no children, otherwise the pid of child that exitted
- Exit code of child process is placed in from_child
Interprocess communication
- Exec()ing a program is like calling a function -- it has parameters (arguments) and can return a result (exit code).
- Not very satisfying way to have two processes talk to one another.
- What are other ways that two processes can communicate?
Interprocess communication
- UNIX supports a variety of interprocess communication methods.
- We know from our shell programming about UNIX pipes, in which the output of one process is fed into the input of another.
- We can build pipes ourselves, and will, when we cover chapters 10 and 11.
- Other options include:
- Disk files
- Shared memory
- Named pipe
- We will explore each of them now.
Communication Using Files
- Using files
- Easy to code
- Supports multiple clients
- Uses standard file permissions
- Can span multiple machines (with NFS)
- Might need to worry about race conditions
Communication Using Shared Memory
- Shared memory is a segment of user space that multiple processes can
access.
- The server creates a shared memory segment and then obtains a pointer to the segment.
sd = shmget(id, size, IPC_CREAT|mode_flags);
ptr = shmat(sd, NULL, 0);
strcpy(ptr, "Wednesday 20 April");
- The client opens the segment and then obtains a pointer to it.
Communication Using Shared Memory
- The client opens the segment and then obtains a pointer to it.
sd = shmget(id, 0, 0);
ptr = shmat(sd, NULL, 0);
printf("date is %s\n", ptr);
- Features:
- Supports multiple clients
- Uses standard file permissions
- Data are shared, not copied!
- Transfers without system calls
- Still need to worry about race conditions
IPC Using Named Pipes
- A named pipe is pipe with a regular filename.
- Processes can open, read, and write to it. It acts as a FIFO queue.
- Any process can create a named pipe with mkfifo(3c)
mkfifo(name, mode);
- Server opens fifo for writing and writes data to it.
- Client opens for reading, and reads data out.
- Open blocks if noone at other end.
- Features:
- Pipes between unrelated processes
- Uses standard file permissions
- One client a time only
Example: Named Pipes
- mkfifo(1) is a system command to create named pipes.
- Use other programs to write and read from it.
- In p8, you'll need two such pipes to communicate between two copies of your program.