CS252: Systems Programming Ninghui Li Topic 18: Lab 6: The Process Games.

21
CS252: Systems Programming Ninghui Li Topic 18: Lab 6: The Process Games

Transcript of CS252: Systems Programming Ninghui Li Topic 18: Lab 6: The Process Games.

CS252: Systems Programming

Ninghui Li

Topic 18: Lab 6: The Process Games

The Process Games

• In each process game, between 2 and 5 programs can participate. • The arena program creates (i.e., forks) N processes of each program, where N is at least 10, and N*P is at most 100. • Each program is loaded with N and P given as

arguments.• The total number of processes in the arena is limited to N*P once the arena starts.

The Players

• The goal of a participating program is to make as many processes in the arena to be duplicates of itself.

• A player must thus• Find target• Use /proc, guess PID, etc.

• Kill target (kill)

• Duplicate itself (fork)

Finding Other Processes via /proc

What is /proc?A pseudo-filesystem that acts as an interface to

internal data structures in the kernel

What is it used for?Can be used to obtain information about the

system

Can be used to change certain kernel parameters at runtime.

Why the Pseudo-File System

Create pseudo-filesystem to represent status information and configuration parameters as files

Provides a unified ‘API’ for collecting status information and configuring drivers

Control access through UNIX permissionsNo new libraries needed – simple filesystem calls are

all that is necessaryQuick, easy access via command lineNot version- or configuration-specific

Process Information

Each process has a /proc directory identified by its PID - /proc/PID/

Symlink /proc/self/ points to the process reading the file system

Allows access toProcess statusProcess memory informationLinks to cwd, exe, root dirPending signalsMany other information

Process Information (Example)

Accessing /proc

• Use standard filesystem API• Including

openat/getdents/opendir/readdir/readlink/read

• Use /proc specific api• I.e., openproc/readproc/readproctab

• See, e.g., source code of pkill/pgrep at https://github.com/soarpenguin/procps-3.0.5/blob/master/pgrep.c

Friend or Foe?

• Possible strategies• Check whether /prof/pid/exe matches argv[0]

• Use IPC between friends• E.g, use shared memory as a bulletin board shared

by all friends

• E.g., use signals

• Possibly others

• Don’t care

• ….

How to Coordinate?

• Global coordination (E.g., shared memory)

• Local coordination• Each process makes decision based on local

information

• No coordination

Strategic Thinking

• Fast and furious versus methodical • Is the overhead worthwhile?

• Should one use different strategies based on values of N, P?

• Should one have different instances of the players adopt different strategies?

Other Things to Explore

• Understand what existing players do?

Use strace to find out the system calls made by a player, and experiment to understand relative performances • The result may be surprising,

• Understand process scheduling and system calls made• Possibly use auditctl

Grading

Base score = Avg score over applicable testing scenarios • In each scenario, Avg of middle 3 performances,

each measured as the percentage of remaining processes after 10 seconds,

1. vs. fork (N=10, 20, 30, 40, 50)

2. vs. digger (N=10, 20, 30, 40, 50)

3. vs. sniper (N=10, 20, 30, 40, 50)

4. vs. phalanx (N=10, 20, 30, 40, 50)

5. vs. hitman (N=10, 20, 30, 40, 50)

Grading (Continued)

• 6. vs. digger + sniper (N=10, 15, 20, 25, 30)

• 7. vs. phalanx + sniper (N=10, 15, 20, 25, 30)

• 8. vs. hitman + phalanx (N=10, 15, 20, 25, 30)

• Team of 4 (all 8 scenarios)

• Team of 3 (scenarios 1-7)

• Team of 2 (scenarios 1-6)

• Team of 1 (scenarios 1-5)

Grading: Extra Credit

10% extra for winning against flash(N=10, 20, 30, 40, 50)

Top 8 joins a tournament, receiving

10%, 8%, 6%,5%,4%,3%,2%,1% extra credit

How to Set UP?

• Install Oracle Virtual Box

• Download the VM image

• Log in as cs252

• Commands• cd lab6, su arena1 or arena2

• ./arena 20 ./fork ./prog

• sudo bash

• top –u arena1

• pgrep –lu arena1 prog show remaining processes

• pkill –KILL –u arena1

Submit

• Use turnin to submit

Clicker Question 1 (signals)

Which signal is sent when the Child process terminates?

A. SIGINIT 

B. SIGCHLD  

C. SIGKILL 

D. SIGSTOP

E. None of the above

Clicker Question 2 (Signals)

Which of the following signal cannot be handled or ignored?

A. SIGINT 

B. SIGCHLD  

C. SIGKILL 

D. SIGALRM

E. None of the above

Clicker Question 3 (Signals)

 Which system call(s) cause the sending of a signal? 

A. kill

B. signal 

C. sigaction

D. Both A and B

E. Both B and C

Clicker Question 4 (Signals)What is the output of the code below?

void handler ( int signum) {printf(“Handled signal\n”)

}

int main() {int pid;signal (SIGKILL, handler);pid = fork();if (pid==0) {     kill(getppid(), SIGKILL);     exit(0);} else {     sleep(20);  }return 0;

}

A. Error child cannot send a SIGKILL signal to parent.

B. Parent goes to the signal handler, prints “handled signal” and goes back to sleep

C. Parent goes to the signal handler, prints “handled signal” and exits

D. Parent sleeps for 20 seconds, without going to signal handler

E. Parent exits without going to the signal handler