process outline

34
Process outline What is process? Process in memory, process state, process control block, threads. Process scheduling: scheduling queue, schedulers. Operations on processes: process creation, process termination. Interprocess communication: shared memory, message passing . Client-server systems: sockets, remote procedure calls, pipes. Homework: Server/Client programming

Upload: others

Post on 22-Mar-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Process outlineWhat is process?

Process in memory, process state, process control block, threads.

Process scheduling: scheduling queue, schedulers.

Operations on processes: process creation, process termination.

Interprocess communication: shared memory, message passing.

Client-server systems: sockets, remote procedure calls, pipes.

Homework: Server/Client programming

ProcessA process (job) is an executable program which is submitted to OS.OS manages the process from its birth to death (error handling).

heap

stack

data

code

Function calls

Dynamic allocation

Global variables

Code Note:An executable file can producemultiple processes.Process in memory

Process state

ready running

new terminated

waiting

admittedinterrupt

exit

scheduler dispatchI/O or event waitI/O or event wait

completion

Process control block

process state

process number

register

memory limits

list of open files

â€Ķ

PCB

In brief, the PCB simply serves as the repository for any information that may vary from process to process (PCB is a data structure).

â€Ē Process stateâ€Ē Program counterâ€Ē CPU registersâ€Ē CPU-scheduling informationâ€Ē Memory-management informationâ€Ē Accounting informationâ€Ē I/O status information

See also Figure 3.4 (p. 105)

Scheduling queuesJob queue: all processes in the system.Ready queue: processes that are residing in main memory.Device queue: processes that are waiting for I/O devices.

Once the process is allocated the CPU and is executing, one of several events could occur:

1. issue an I/O request and then be placed in the device queue2. create a new subprocess and wait for subprocess’s termination3. removed forcibly from the CPU, as a result of an interrupt,

and then put back in the ready queue.

See Figure 3.6 – 3.7 (p. 107 – 108)

Ready = in memory

SchedulersLong-term scheduler (Job scheduler), loading a process into memory.

execute less oftencontrol the degree of multi-programming (#processes in memory)select a good process mix of I/O-bound and CPU-bound processes

Short-term scheduler (CPU scheduler), allocate CPU to one of them.execute very often (every 100 milliseconds)if the scheduler takes 10 milliseconds to decide to execute a process

for 100 milliseconds, then 10/(100 + 10) = 9% overheads

Medium-term scheduler (swapping)remove processes from memory (if advantageous)when memory requirement overcommitted available memorysee Figure 3.8

Context switch save unloaded process and restore loaded process

P1 CPUscheduling

P2Save P1āļĨāļ‡ mem

Context switchRestore P2

āđƒāļŠāđˆ cpu

Process CreationParent / child / pid (See Figure 3.9 Solaris system, “ps” and “kill” command)

Concurrent vs parallel execution

fork / exec (see Figure 3.10, 3.11, p. 113)

wait() – system call

WaitForSignalObject() – Win32 API

// compile: gcc fork.c -o fork// run: ./fork āļŦāļĢāļ·āļ­ ./a.out

#include <sys/types.h>#include <stdio.h>#include <unistd.h>

int main() {

pid_t pid = fork();

if (pid < 0) {fprintf(stderr, "Fork Failed");return 1;

} else if (pid == 0) {execlp("/bin/ls", "ls", NULL);

} else {wait(NULL);printf("Child Complete");

}return 0;

}

Parent

Child

Error! no child

āļāļīāļˆāļāļĢāļĢāļĄ #1 (āļ— āļģāđƒāļ™ Linux āđ€āļ—āđˆāļģāļ™āļąāļ™āđ‰)āđāļāđ‚āđ‰āļ›āļĢāđāļāļĢāļĄāļ™āļĩāđƒāđ‰āļŦāļŠāđ‰āļĢāļģāđ‰āļ‡āļĨāļāļđ 10 āļ•āļ§āļą āļĨāļāļđāđāļ•āđˆāļĨāļ°āļ•āļ§āļąāļžāļĄāļīāļžāļ‚āđŒāļ­āđ‰āļ„āļ§āļģāļĄāļ”āļ‡āļąāļ™āļĩāđ‰(āļ”āļŦāļđāļ™āļģāđ‰āļ–āļ”āļąāđ„āļ›)

Optional āđƒāļŦāļĨāđ‰āļāļđāđāļ•āđˆāļĨāļ°āļ•āļ§āļąāļŠāđˆāļ§āļĒāļāļ™āļąāļ”āļģāļ§āļ™āđ‚āđŒāļŦāļĨāļ”āđ„āļŸāļĨāļŦāđŒāļĨāļģāļĒ āđ† āđ„āļŸāļĨāđŒ āļŦāļĢāļ­āļ·āđ„āļŸāļĨāđ€āđŒāļ”āļĩāļĒāļ§āđāļ•āđˆāļŠāđˆāļ§āļĒāļāļ™āļąāđ‚āļŦāļĨāļ”āļ„āļ™āļĨāļ°āļŠāđˆāļ§āļ™ āļšāļ™āļąāļ—āļāļķāđāļ•āđˆāļĨāļ°āļŠāđˆāļ§āļ™āļĨāļ‡āđ€āļ›āđ‡āļ™āđ„āļŸāļĨ āđŒāđāļĨāļ§āđ‰āđƒāļŦāļžāđ‰āļ­āđˆāļĢāļ§āļĄāđ„āļŸāļĨāļ—āđŒāļąāļ‡āđ‰āļŦāļĄāļ”āđƒāļŦāđ€āđ‰āļ›āđ‡āļ™āđ„āļŸāļĨāđ€āđŒāļ”āļĩāļĒāļ§

āļšāļ™ Linux āđƒāļŠāļ„āđ‰ āļģāļŠāļąāđˆāļ‡ man āđ€āļŠāđˆāļ™ man fork āļŦāļĢāļ­āļ· man waitāđ€āļžāđˆāļ·āļ­āđ€āļĢāļĒāļĩāļāļ” āļđdocument āļ‚āļ­āļ‡āļŸāļąāļ‡āļāļŠāđŒāļ™āļąāđƒāļ™āļ āļģāļĐāļģ C

if (pid < 0) {fprintf(stderr, "Fork Failed");return 1;

} else if (pid == 0) {execlp("/bin/ls", "ls", NULL);

} else {wait(NULL);printf("Child Complete");

}

if (pid < 0) {fprintf(stderr, "Fork Failed");return 1;

} else if (pid == 0) {execlp("/bin/ls", "ls", NULL);

} else {wait(NULL);printf("Child Complete");

}

Parent process (pid = child)

Child process (pid = 0)

Exec āļ„āļ­āļ·āđ‚āļŦāļĨāļ” program āđƒāļŦāļĄāđˆāļĄāļēāļ—āļąāļš

āļ—āļĩāļ—āđˆ āļēāđāļšāļšāļ™āļĩ āđ‰āđ€āļžāļĢāļēāļ°āđ€āļ‚āļĒāļĩāļ™āđ‚āļ›āļĢāđāļāļĢāļĄ OS āļ‡āđˆāļēāļĒfork āļāđ‡āļ„āļ­āļ·āļāļēāļĢ duplicate āļŦāļ™āđˆāļ§āļĒāļ„āļ§āļēāļĄāļˆ āļē

Unix / Linux

āļ•āļ§āļąāļ­āļĒāđˆāļēāļ‡ output āļ—āđˆāļĩāļ•āđ‰āļ­āļ‡āļāļēāļĢI'm the child number 0 (pid 25580)

I'm the child number 1 (pid 25581)

I'm the child number 2 (pid 25582)

I'm the child number 3 (pid 25583)

I'm the child number 4 (pid 25584)

I'm the child number 5 (pid 25585)

I'm the child number 6 (pid 25586)

I'm the child number 7 (pid 25587)

I'm the child number 8 (pid 25588)

I'm the child number 9 (pid 25589)

Parent terminates (pid 25579)

āđƒāļŠāļŸāļąāđ‰āļ‡āļāļŠāđŒāļ™āļą getpid()

āļŦāļĒāļ”āļļāļĢāļ­āļˆāļ™āļāļĢāļ°āļ—āļąāđˆāļ‡āļĨāļāļđāļ•āļ§āļąāđƒāļ”āļ•āļ§āļąāļŦāļ™āļķāđˆāļ‡āļ— āļģāļ‡āļģāļ™āđ€āļŠāļĢāļˆāđ‡āđ„āļĄāđˆāđƒāļŠāđˆāļĢāļ­āļˆāļ™āļĨāļāļđāļ—āļāļļāļ•āļ§āļąāļ— āļģāļ‡āļģāļ™āđ€āļŠāļĢāļˆāđ‡ āļ­āđˆāļģāļ™āđ€āļ­āļāļŠāļģāļĢāļ”āļĩ āđ†

// compile: MS Visual C++ 2008 Express

#include "stdafx.h"#include "windows.h“int _tmain(int argc, _TCHAR *argv[]) {

STARTUPINFO si;PROCESS_INFORMATION pi;ZeroMemory(&si, sizeof(si));si.cb = sizeof(si);ZeroMemory(&pi, sizeof(pi));LPTSTR szCmdline = _tcsdup(TEXT("c:\\windows\\system32\\mspaint.exe"));if (!CreateProcess(NULL, szCmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {

fprintf(stderr, "Create Process Failed");}WaitForSingleObject(pi.hProcess, INFINITE);printf("Child Complete");CloseHandle(pi.hProcess);CloseHandle(pi.hThread);return 0;

}

Windows

āļ āļģāļĐāļģ C āļ‚āļ­āļ‡āđāļ•āđˆāļĨāļ° OS āļ„āļĨāļģāđ‰āļĒāļāļ™āļąāđāļ•āđˆāđ„āļĄāđˆāđ„āļ”āđ€āđ‰āļŦāļĄāļ·āļ­āļ™āļāļ™āļąāļ—āļąāļ‡āđ‰āļŦāļĄāļ” āđ‚āļ”āļĒāđ€āļ‰āļžāļģāļ° C Libraryāđ€āļŠāđˆāļ™ Linux āļĄāļĩ fork() āđāļ•āđˆ Windows āđ„āļĄāđˆāļĄāļĩWindows āļĄāļĩ CreateProcess() āđāļ•āđˆ Linux āđ„āļĄāđˆāļĄāļĩ

Process Terminationexit() – system call

All the resources of the process – including physical and virtual memory, open files, and I/O buffers – are deallocated by the operating system.

TerminateProcess() – Win32 API

A parent may terminate the execution of one of its children for a variety of reasons, such as these:

1) the child has exceeded its usage of some of the resources that it has been allocated.

2) the task assigned to the child is no longer required.3) the parent is exiting, and the OS does not allow a child to continue

if its parent terminates (cascading termination).

Interprocess communicationThere are several reasons for providing an environment that allows process cooperation:

Information sharing shared file (lock/unlock)

Computation speed parallel sortingModularity web browser ( 1 web = 1 process)Convenience copy & paste, drag & drop

Cooperating processes require an “interprocess communication (IPC).”

Shared Memory vs. Message Passing (Figure 3.13, p. 117)

Shared-memory systemsProducer-consumer problem (āļŠ āļēāļŦāļĢāļąāļš users āļ—āļąāđˆāļ§āđ„āļ› āļŠāļ­āļ™āđƒāļŠāđ‰ pipe āļšāļ™ shell āļāđ‡āļžāļ­)āđ€āļŠāđˆāļ™ cat /usr/share/dict/american-english | grep board | wc -lāđ€āļĢāļēāļŠāļēāļĄāļēāļĢāļ–āđ€āļ‚āļĒāļĩāļ™āđ‚āļ›āļĢāđāļāļĢāļĄ āđ€āļŠāđˆāļ™ āļ āļēāļĐāļē C āđƒāļŦāđ‰āļŠāļĢāđ‰āļēāļ‡ pipe āđ€āļžāļ·āļ­āđˆāđ€āļŠāļ·āļ­āđˆāļĄāļĢāļ°āļŦāļ§āđˆāļēāļ‡ 2 processes āđ„āļ”āđ‰ āļ āļēāļĐāļēāļ­āļ·āļ™āđˆāļ•āđ‰āļ­āļ‡āļ”āļđāļ§āđˆāļēāļĄāļĩ APIs āđƒāļŦāđ‰āļŦāļĢāļ·āļ­āđ„āļĄāđˆ?

#define BUFFER_SIZE 10

typedef struct {â€Ķ â€Ķ â€Ķ

} item// circular queueitem buffer[BUFFER_SIZE];int in = 0; // tailint out = 0; // head

Shared memory (queue)

item nextProduced;

while (true) {while (((in + 1) % BUFFER_SIZE) == out) { }buffer[in] = nextProduced;in = (in + 1) % BUFFER_SIZE;

}

Producer process

item nextConsumed;

while (true) {while (in == out) { }nextConsumed = buffer[out];out = (out + 1) % BUFFER_SIZE;

}

Consumer process

Pipe and Redirect (one-way communication)

â€Ē In C programming, there are 3 stardard I/O channels.Standard input (stdin) = keyboardStandard output (stdout) = monitorStandard error (stderr) = monitor

Pipe is used to connect stdout (one process) to stdin (another)For example, “ls | more” or “tar -c myfolder | gzip -c > myfolder.tar.gz”

â€Ē Redirect is used to redirect input or outputFor example, “a.out > file.txt” or “a.out >> file.txt” (append).āļ–āļģāđ‰āđƒāļŠāļ•āđˆāļ§āļąāđ€āļĨāļ‚ āđ€āļŠāđˆāļ™ 1> āļŦāļĢāļ­āļ· 2> āļŦāļĄāļģāļĒāļ–āļķāļ‡ āļāļĢāļ­āļ‡āđ€āļ­āļģāđ€āļ‰āļžāļģāļ° stdout āđāļĨāļ° stderr

2>&1 āļŦāļĄāļģāļĒāļ–āļķāļ‡ āđƒāļŦāđ€āđ‰āļ­āļģ stderr āđ„āļ›āļ­āļ­āļāļ—āļĩāđˆ stdout āđāļ—āļ™

â€Ē < gets input from fileFor example, “tar -xvz < filename.tar.gz”

â€Ē << gets input from the shell file that is currently executing.āđ„āļĄāđˆāļ„āļ­āđˆāļĒāđ„āļ”āđƒāđ‰āļŠāđ‰

fprintf(stdout, "%s", "Hello world\n");fprintf(stderr, "%s", “Error message\n");

// compile: gcc shared_mem.c

#include <sys/types.h>#include <sys/shm.h>#include <sys/stat.h>#include <stdio.h>#include <unistd.h>

int main() {

int segment_id;char *shared_memory;

segment_id = shmget(IPC_PRIVATE, 4096, S_IRUSR | S_IWUSR);shared_memory = (char *)shmat(segment_id, NULL, 0);

sprintf(shared_memory, "Hi there!");

// continue on the next page

Adopted from Figure 3.16 (p. 125)

pid_t pid;

pid = fork();

if (pid == 0) { // child

printf("*%s\n", shared_memory);

} else { // parent

wait(NULL);shmdt(shared_memory); // detachshmctl(segment_id, IPC_RMID, NULL); // remove

}

return 0;}

Client-Server SystemsSockets, ports

See Figure 3.19 – 3.20 (p. 130 – 131)

sock.accept() is a blocking method (usually spawn a process or a thread).

127.0.0.1 is loop back.

Sockets is low-level method of communication (cannot impose a structure on the data). Think about passing a tree from one computer to another!

VMware can create a virtual network that connects VMs together (Team),suitable for developing and testing network applications (e.g. NFS).

āļāļīāļˆāļāļĢāļĢāļĄ #2 (āļ— āļģāđƒāļ™ Linux āđ€āļ—āđˆāļģāļ™āļąāļ™āđ‰)āļ•āļ­āđ‰āļ‡ new process āļŦāļĢāļ­āļ· spawn threadāđ€āļžāđˆāļ·āļ­āđƒāļŦāļšāđ‰āļĢāļāļīāļģāļĢ client āđ„āļ”āļŦāđ‰āļĨāļģāļĒ āđ† āļ•āļ§āļąāļžāļĢāļ­āđ‰āļĄāļāļ™āļą

āļ§āļ™ loop āļŠāđˆāļ‡āļ‚āļ­āđ‰āļĄāļĨāļđāļ§āļ™āļąāđ€āļ§āļĨāļģāļ›āļąāļˆāļˆāļšāļļāļ™āļąāđ„āļ›āđ€āļĢāļ·āđˆāļ­āļĒ āđ†

Loop back āđāļĨāļ° port number

āđƒāļŠāđ‰āļ āļēāļĐāļēāļ­āļ·āļ™āđˆ āđ† āļāđ„āđ‡āļ” āđ‰āļ— āļēāļšāļ™ Linux āđ€āļ—āļēāđˆāļ™āđ‰āļąāļ™

Server

Client

Client

āļ§āļ™āļąāđ€āļ§āļĨāļģāļ›āļąāļˆāļˆāļšāļļāļ™āļą

āļ§āļ™āļąāđ€āļ§āļĨāļģāļ›āļąāļˆāļˆāļšāļļāļ™āļą

Print āļ§āļ™āļąāđ€āļ§āļĨāļģāļ­āļ­āļāļĄāļģāđ€āļĢāļ·āđˆāļ­āļĒ āđ†

Print āļ§āļ™āļąāđ€āļ§āļĨāļģāļ­āļ­āļāļĄāļģāđ€āļĢāļ·āđˆāļ­āļĒ āđ†

āļ­āļģāļˆāļˆāļ°āļŠāđˆāļ‡āļ‚āļ­āđ‰āļĄāļĨāļđāļ§āļ™āļąāđ€āļ§āļĨāļģāļ—āļāļļ āđ† 1 āļ§āļīāļ™āļģāļ—āļĩāđ€āļžāđˆāļ·āļ­āđ„āļĄāđˆāđƒāļŦāđ€āđ‰āļĢāļ§āđ‡āđ€āļāļīāļ™āđ„āļ›

āļ•āļ­āđ‰āļ‡āđ€āļ‚āļĩāļĒāļ™āđ‚āļ›āļĢāđāļāļĢāļĄāđƒāļŦ āđ‰fork (āļŠāļĢāļģāđ‰āļ‡ process āđƒāļŦāļĄ)āđˆ āđ€āļžāđˆāļ·āļ­āļšāļĢāļāļīāļģāļĢ client āđāļ•āđˆāļĨāļ°āļ•āļ§āļąāļ–āļķāļ‡āļˆāļ°āđ€āļŦāđ‡āļ™āļ§āđˆāļģ client āļ—āļāļļāļ•āļ§āļąāđ„āļ”āļĢāđ‰āļšāļąāļšāļĢāļāļīāļģāļĢāđ„āļ›āļžāļĢāļ­āđ‰āļĄ āđ† āļāļ™āļą

Message PassingMP is useful in distributed environment, where the communicating processes may reside on different computers connected by a network (e.g. Cluster).

OS āļŠāļĢāļģāđ‰āļ‡ message queue āļ— āļģāļŦāļ™āļģāđ‰āļ—āđˆāļĩāļ„āļĨāļģāđ‰āļĒ āđ† āļāļšāļą mail boxāđāļšāļš 1. āļŦāļĨāļģāļĒ āđ† process āļšāļ™āđ€āļ„āļĢāļ·āđˆāļ­āļ‡āđ€āļ”āļĩāļĒāļ§āļāļ™āļą āļĄāļĩ mail box āļŠāļ”āļļāđ€āļ”āļĩāļĒāļ§āđāļšāļš 2. āļĄāļĩāļŦāļĨāļģāļĒ āđ† āđ€āļ„āļĢāļ·āđˆāļ­āļ‡ āđāļ•āđˆāļĨāļ°āđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļ•āļ­āđ‰āļ‡āļĄāļĩ mail box āļ‚āļ­āļ‡āļ•āļ§āļąāđ€āļ­āļ‡

Remote Procedure Calls

Programmer’sview

What actuallyhappen

āļ„āļĨāļģāđ‰āļĒāđ† virtualization āļŦāļĨāļ­āļāļ§āđˆāļģāļŸāļąāļ‡āļāļŠāđŒāļ™āļąāļ™āļąāļ™āđ‰ execute āļ­āļĒāļđāđˆāļšāļ™āđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļˆāļĢāļ‡āļīāđ†

Why RPC

â€Ē If no RPC, you have to use socket programming and you have to impose a structure on data! (Java uses serialization)

â€Ē Only one copy of a function, updating a service at one place,i.e. banking application.

â€Ē Distributing jobs to many servers. Each server is good at a particular job. Parallel programming!

â€Ē Easy interprocess communication (no socket programming, no imposing data), i.e. e-exam (multiple choices).

Server

Client Client Client Client

string LoadQuestion(int i)string LoadChoice(int i, int j)bool Save(string id, int i, int j)

Faster coding compared tosocket programming orweb programming.

Today’s RPCâ€Ē .NET Remotingâ€Ē Java RMI (remote method invocation)

āļ‚āđ‰āļ­āđ€āļŠāļĩāļĒāļ„āļ­āļ·āļ—āļąāļ‡āđ‰ server āđāļĨāļ° client āļ•āđ‰āļ­āļ‡āđ€āļ›āđ‡āļ™ platform āđ€āļ”āļĒāļĩāļ§āļāļąāļ™

Web Servicesâ€Ē āđƒāļŠāļ āđ‰āļģāļĐāļģāļ­āđˆāļ·āļ™ āđ† āļ™āļ­āļāļˆāļģāļ C# āļāđ‡āđ€āļ‚āļĩāļĒāļ™ web service āđ„āļ”āđ‰â€Ē āđ€āļĢāļĒāļĩāļāđƒāļŠ āđ‰web service āļˆāļģāļāļ āļģāļĐāļģāđƒāļ”āļāđ‡āđ„āļ”āđ‰â€Ē āđāļāļ›āļąāđ‰āļāļŦāļģāļ‹āļ­āļŸāļ•āđāđŒāļ§āļĢāđ€āđŒāļ–āđˆāļ·āļ­āļ™ āļ„āļīāļ”āļ„āđˆāļģāļšāļĢāļāļīāļģāļĢāļˆāļģāļāļˆ āļģāļ™āļ§āļ™āļ„āļĢāļąāļ‡āđ‰āļ—āļĩāđˆāđƒāļŠāļ‡āđ‰āļģāļ™ āļŦāļĢāļ­āļ·āļĢāļ°āļĒāļ°āđ€āļ§āļĨāļģāļ—āđˆāļĩāđƒāļŠāļ‡āđ‰āļģāļ™āđ„āļ”āđ‰

<%@WebService Language="C#" Class="MyService"%>

using System.Web.Services;

public class MyService : WebService {

[WebMethod]public int Add(int a, int b) {

return a + b;}

}

MyService.asmx(in a virtual directory)

Client(MyService.dll)

using System;using MyNameSpace;

class MyProg {

public static void Main(string [] Args) {

MyService S = new MyService();Console.WriteLine(S.Add(1,2));

}}

http://localhost/test/MyService.asmx?op=Add

Process Communicationāļ›āļąāļˆāļˆāļšāļļāļ™āļąāđ€āļĢāļģāļĄāļāļąāļˆāļ°āļ•āļ­āđ‰āļ‡āđ€āļ‚āļĩāļĒāļ™āđ‚āļ›āļĢāđāļāļĢāļĄāļ‚āļ­āļ‡āđ€āļĢāļģāđƒāļŦ āđ‰communicate āļāļšāļąāđ‚āļ›āļĢāđāļāļĢāļĄāļŠ āļģāđ€āļĢāļˆāđ‡āļĢāļđāļ›āļ­āļ·āđˆāļ™āđ† (āļ‹āļķāļ‡āđˆāļ­āļģāļˆāļˆāļ°āļ­āļĒāļđāļšāđˆāļ™āđ€āļ„āļĢāļ·āđˆāļ­āļ‡āđ€āļ”āļĩāļĒāļ§āļāļ™āļąāļŦāļĢāļ­āļ·āļ„āļ™āļĨāļ°āđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļāđ‡āđ„āļ”)āđ‰

.NET (C#) simplified code

ExcelApp = new Excel.ApplicationClass();ExcelBook = ExcelApp.Workbooks.Open(Filename, â€Ķ, â€Ķ, â€Ķ);ExcelSheet = ExcelBook.Worksheets.get_Item(1);

// writeExcelSheet.Cells[row,col] = “Chatchawit”;

// readname = ExcelSheet.Cells[row,col];

ExcelBook.Close(true, Missing.Value, Missing.Value);ExcelApp.Quit(); āļ›āļąāļˆāļˆāļļāļšāļ™āļąāļŠāļēāļĄāļēāļĢāļ–āļ­āđˆāļēāļ™āđ€āļ‚āļĩāļĒāļ™āđ„āļŸāļĨ āđŒExcel āđ„āļ”āđ‰āļ•āļĢāļ‡ āđ† āđ€āļĨāļĒ

āļŠāļĢāļģāđ‰āļ‡ process āļĢāļ™āļą Excel

Homework

POSIX Message Passing (p. 148)

- write “central.c” and “external.c”- 1 copy of “central” and 4 copies of “external” can be started in any order.- begin from 2 processes and one-way communication.- then, two-way communication (2 mailboxes) and 5 processes (5 mailboxes).- use 5 terminals for the ease of debugging.- always check and clear message queues by “ipcs” and “iprm -q qid.”

central.c external.c

āđ‚āļˆāļ—āļĒāđŒ

Central

size āļ‚āļ­āļ‡ message āļ—āđˆāļĩāļˆāļ°āļŠāđˆāļ‡āđƒāļ™āļŸāļąāļ‡āļāļŠāđŒāļ™āļą msgrcv āļ™āļšāļąāđāļ„āđˆāļ™āļĩ āđ‰

āļšāļ‡āļąāļ„āļšāļąāļ§āđˆāļģāļ•āļ§āļąāđāļĢāļāđƒāļ™ struct āļ•āļ­āđ‰āļ‡āđ€āļ›āđ‡āļ™ message type(āļŦāļĢāļ­āļ·āļˆāļ°āļĄāļ­āļ‡āļ§āđˆāļģāđ€āļ›āđ‡āļ™ priority āļāđ‡āđ„āļ”)āđ‰

āđāļ›āļĨāļ‡ string āđ€āļ›āđ‡āļ™ int

āļ­āļ“āļļāļŦāļ āļĄāļđāļīāđ€āļĢāļīāļĄāđˆāļ•āļ™āđ‰

msqid āļ„āļ·āļ­ message queue id, 70 āļ„āļ·āļ­ queue name (users 2 āļ„āļ™āļ­āļģāļˆāļˆāļ°āđƒāļŠ āđ‰queue name āļ‹ āļģāđ‰āļāļ™āļąāđ„āļ”)āđ‰0600 āļ„āļ·āļ­āļĒāļ­āļĄāđƒāļŦ āđ‰process āļ‚āļ­āļ‡āļœāļđāļŠāđ‰āļĢāļģāđ‰āļ‡āļ­āđˆāļģāļ™āđ€āļ‚āļĩāļĒāļ™ mailbox āđ„āļ”āđ‰IPC_CREAT āļ„āļ·āļ­ āļ–āļģāđ‰āļĒāļ‡āļąāđ„āļĄāđˆāļĄāļĩāđƒāļŦāļŠāđ‰āļĢāļģāđ‰āļ‡āđƒāļŦāļĄāđˆ

āļ‚āļ­ message queue id āļ‚āļ­āļ‡ external 4 āļ•āļ§āļąwhile loop āđ€āļžāđˆāļ·āļ­āļĢāļ­āđƒāļŦ āđ‰external āļŠāļĢāļģāđ‰āļ‡

blocking

0 āļ­āđˆāļģāļ™ first message āļšāļ™ queue>0 āđ€āļŠāđˆāļ™ 2 āđ€āļ­āļģ first message āļ—āđˆāļĩ priority = 2<0 āđ€āļŠāđˆāļ™ -2 āđ€āļ­āļģ first message āļ—āđˆāļĩ priority <= 2

blocking

updatecentral

updateexternal

1 central + 4 external

remove āđƒāļŠāļāđ‰āļĢāļ“āļĩ IPC_STATIPC_SET