1 / 31

Module 2

Module 2. Programming with Processes. Processes. Process -- a program in execution May share code segments Typically do not share data, stack, heap OS data structures (file handles, coroutine, context block) kept in struct named User One User struct per process. Login.

Download Presentation

Module 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Module 2 Programming with Processes

  2. Processes Process -- a program in execution May share code segments Typically do not share data, stack, heap OS data structures (file handles, coroutine, context block) kept in struct named User One User struct per process

  3. Login • Authenticates via name/password • Name converted to number(s) • User Id • Group Id : used for sharing Macintosh-2:~ shawne$ ls -l total 48 drwx------+ 89 shawne staff 3026 Mar 1 14:03 Desktop drwx------+ 7 shawne staff 238 Feb 13 15:50 Documents drwx------+ 5 shawne staff 170 Feb 19 11:07 Downloads User id group id

  4. Process Creation and Destruction int execle(const char *filepath,const char *arg, ..., (char *)0, char *const envp[] );/* replaces caller's memory image with "program" */void exit(int status);/* terminates execution; returns status to parent */ pid_t wait(int *stat_loc);pid_t waitpid(pid_t pid, int *stat_loc, int options);/* blocks caller until a child terminates.pid_t fork(void);/* duplicates the memory image of the caller */

  5. #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { int pid, out; char *envp[]={"ME=BOB",0}; pid=fork(); printf("I am %d\n",pid); if (pid!=0) { wait(&out); printf("child returned %d\n",out); execle("/bin/echo","dog","cat",0,envp); } exit(55); }

  6. C program usesOS APIs to write commands Shell programs Window programs Browser Browser programs

  7. UNIX Shell

  8. Boot-up to Command

  9. Remote Executionssh user@hostname command • Automatic login is desirable • The scheme is based on public-key cryptography, using cryptosystems where encryption and decryption are done using separate keys, and it is infeasible to derive the decryption key from the encryption key. • The idea is that each user creates a public/private key pair for authentication purposes. The server knows the public key, and only the user knows the private key. • Private key encrypts login info; public key decrypts

  10. Data Parallelism • Why: speed-up • How (greatly simplified): • split work into independent similar pieces • execute pieces in parallel • collect results • Simple example • Count number of identifiers in a set of files 6/16/2010 10 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

  11. Count ids in several files./test foo.txt moo.txt boo.txt #include <unistd.h> #include <stdio.h> #include <sys/wait.h> int main(int argc, char *argv[]) { int count=0, i; for (i=1; i<argc; i++) { if (fork()) continue;execl("./cnt","./cnt",argv[i],0); } //for for (i=1; i<argc; i++) { int cnt; wait(&cnt); count+=WEXITSTATUS(cnt); } printf("count = %d¥n", count); exit(0); }

  12. Shell procedure to count ids declare -i x=0 for i in $* ; do x=x+`./cnt $i` ; echo $x ; Done Command Line ./foo.sh /Users/bobcook/Desktop/*.txt Output 318 2605 3141

  13. Amdahl’s hypothesis Assume that sorting takes 70% of the execution time of a sequential program You replace the sorting algorithm with one that scales perfectly on multi-core hardware How many cores do you need to get a 4x speed-up of the program? 6/16/2010 14 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

  14. Amdahl’s Formula 6/16/2010 15 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

  15. f = 10% Amdahl’s law limit, just 1.11x Speedup achieved with perfect scaling 6/16/2010 16 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

  16. f = 70% Desired 4x speedup Limit as c→∞ = 1/(1-f) = 3.33 Speedup achieved (perfect scaling on 70%) 6/16/2010 17 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

  17. f = 98% 6/16/2010 18 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

  18. Lesson Speedup is limited by sequential code Even a small percentage of sequential code can greatly limit potential speedup 6/16/2010 19 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

  19. Inter-Process Communication (IPC) Reasons for IPC Information sharing Program speedup Fault tolerance Resource limitations per node Heterogeneity Modularity Convenience Privilege separation -- different levels of protection

  20. Common IPC APIs Pipes Message queues Shared memory Shared files Signals Sockets (TCP/IP protocols) Message passing (MPI, discussed later) Semaphores (discussed later) Remote variable access (discussed later) Remote procedure call (discussed later) Remote process invocation (ssh commands)

  21. Pipe Command Example(invoke commands from C programs) #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> int main() { //pipe input to sort command   FILE *write;  char *str[]={"hi", "by", "so", "mo"};  int i;   write = popen("sort", "w");   if (write !=NULL) {     for (i=0; i<4; i++) fprintf(write, "%s\n", str[i]);    pclose(write);     exit(EXIT_SUCCESS);   }   exit(EXIT_FAILURE); } OUTPUT by hi mo so

  22. Pipe System Call API #include <unistd.h> int pipe(int fildes[2] /* out parameter*/); fildes[0] - read end of pipe fildes[1] - write end of pipe Be aware that write() can write less than you request and read() can read less. File handles are inherited by fork()/exec() processes.

  23. FIFO File Object System API #include <sys/types.h> #include <sys/stat.h> int mkfifo(const char *path, mode_t mode); Like a file, a FIFO can be opened for reading or writing or both intermittently.

  24. Message Queue API

  25. Message Queue -record oriented #include <fcntl.h> /* For O_* constants */ #include <sys/stat.h> /* For mode constants */ #include <mqueue.h> struct mq_attr { long mq_flags; /* Flags: 0|O_NONBLOCK */ long mq_maxmsg; /* Max. # of msgs on Q*/ long mq_msgsize; /* Max. msg size (bytes) */ long mq_curmsgs; /* # msgs currently in Q*/ }; mqd_t mq_open(const char name[], int oflag); //to open only mqd_t mq_open(const char name[], int oflag, mode_t access_mode, struct mq_attr *attr); int mq_close(mqd_t mqdes); int mq_unlink(const char name[]); int mq_send(mqd_t mqdes, const char msg_ptr[], size_t msg_len, unsigned msg_priority); ssize_t mq_receive(mqd_t mqdes, char msg_ptr[], size_t msg_len, unsigned *msg_priority);

  26. Processing Multiple Files in Parallel One thread per Chunk

  27. Shared Memory Among Multiple Processes

  28. Shared Memory API //returns shmid for new segment of size bytes int      shmget(key_t key, size_t size, int mode_t); //control ops such as stat, unlink, lock, unlock int      shmctl(int shmid, int cmd, struct shmid_ds *buf); //attach segment to process at shmaddr void *shmat(int shmid, const void *shmaddr, int shmflag); //detach segment at shmaddr from this process int     shmdt(const void *shmaddr);

  29. Memory-Mapped Files for Increased Efficiency

  30. Memory-Mapped File API #include <sys/mman.h> void *mmap(void *start_addr, size_t length, int protection, int flags, int filedescriptor, off_t offset); int     munmap(void *start_addr, size_t length);

More Related