Environment Variables

Similar documents
CSE 421/521 - Operating Systems Fall 2013 Recitations. Recitation - II Unix Processes Prof. Tevfik Kosar. University at Buffalo

Parameter Passing in C. Pointer Arithmetic. Parameter Passing in C. Pointer Arithmetic. Pointer Arithmetic. Tevfik Ko!ar

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

CSC 1600 Unix Processes. Goals of This Lecture

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Preview. Process Control. What is process? Process identifier The fork() System Call File Sharing Race Condition. COSC350 System Software, Fall

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

What is a Process. Preview. What is a Process. What is a Process. Process Instruction Cycle. Process Instruction Cycle 3/14/2018.

Roadmap. Concurrent Programming. Concurrent Programming. Motivation. Why Threads? Tevfik Ko!ar. CSC Systems Programming Fall 2010

Operating systems and concurrency - B03

CSC Systems Programming Fall Lecture - XVIII Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 11 th, 2008

Dept. of Computer Science & Engineering 1 Knowledge & Data Engineering Lab.

Roadmap. Concurrent Programming. Concurrent Programming. Communication Between Tasks. Motivation. Tevfik Ko!ar

CS240: Programming in C

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

Chapter 3 Processes we will completely ignore threads today

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

Process Management! Goals of this Lecture!

518 Lecture Notes Week 3

CSC Systems Programming Fall Lecture - XV Network Programming - I. Tevfik Ko!ar. Louisiana State University. November 9 th, 2010

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

CS 25200: Systems Programming. Lecture 14: Files, Fork, and Pipes

Using make. Using Variables in Makefiles. Implicit Rules. Static vs Dynamic Libraries. Libraries. Tevfik Ko!ar

Process Management 1

PROCESS PROGRAMMING INTERFACE

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

628 Lecture Notes Week 4

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

CSC Systems Programming Fall Lecture - XXIII Final Review. Tevfik Koşar. Louisiana State University

Process Management! Goals of this Lecture!

Princeton University. Computer Science 217: Introduction to Programming Systems. Process Management

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

COE518 Lecture Notes Week 2 (Sept. 12, 2011)

CSC Systems Programming Fall Lecture - XIV Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 2nd, 2010

A Unix Process. Joseph Cordina

CITS2002 Systems Programming. Creating a new process using fork() 1 next CITS2002 CITS2002 schedule

Process Management 1

Fall 2015 COMP Operating Systems. Lab #3

Processes: Introduction. CS 241 February 13, 2012

System Programming. Pipes I

Computer Systems Assignment 2: Fork and Threads Package

CS240: Programming in C

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

CS 350 : COMPUTER SYSTEM CONCEPTS SAMPLE TEST 2 (OPERATING SYSTEMS PART) Student s Name: MAXIMUM MARK: 100 Time allowed: 70 minutes

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

CSci 4061 Introduction to Operating Systems. Processes in C/Unix

CS 261 Fall Mike Lam, Professor. Processes

fork System-Level Function

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

System Programming. Process Control II

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

Processes. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! Scheduling processes

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

UNIX System Calls. Sys Calls versus Library Func

UNIX System Programming. Overview. 1. A UNIX System. 2. Processes (review) 2.1. Context. Pipes/FIFOs

What Is A Process? Process States. Process Concept. Process Control Block (PCB) Process State Transition Diagram 9/6/2013. Process Fundamentals

Lecture 3 Process API in UNIX systems

System Programming. Process Control III

The Fundamentals. Port Assignments. Common Protocols. Data Encapsulation. Protocol Communication. Tevfik Ko!ar

Maria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort

Operating systems fundamentals - B06

coe628 Final Study Guide (2017)

Operating Systems 2230

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

Notice: This set of slides is based on the notes by Professor Perrone of Bucknell and the textbook authors Silberschatz, Galvin, and Gagne

COSC Operating Systems Design, Fall Lecture Note: Unnamed Pipe and Shared Memory. Unnamed Pipes

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

Assignment 1. Teaching Assistant: Michalis Pachilakis (

Pipes. Pipes Implement a FIFO. Pipes (cont d) SWE 545. Pipes. A FIFO (First In, First Out) buffer is like a. Pipes are uni-directional

CSI Module 2: Processes

Unix Processes 1 / 31

CSC209F Midterm (L0101) Fall 1998 University of Toronto Department of Computer Science

File Descriptors and Piping

University of Washington What is a process?

Operating Systems. Processes

Final Precept: Ish. Slides Originally Prepared by: Wonho Kim

Solutions to the first midterm. COSC 4330/6310 Summer 2013

Operating System Structure

CS 550 Operating Systems Spring Inter Process Communication

CS Operating system Summer Midterm I -- July 11, 2017 You have 115 min (10:00am-11:55pm). Good Luck!

Operating Systems. Lecture 05

Pipes and FIFOs. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Processes. q Process concept q Process model and implementation q Multiprocessing once again q Next Time: Scheduling

IC221: Systems Programming 12-Week Written Exam [SOLUTIONS]

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

The Shell, System Calls, Processes, and Basic Inter-Process Communication

Interacting with Unix

Carnegie Mellon. Processes. Lecture 12, May 19 th Alexandre David. Credits to Randy Bryant & Dave O Hallaron from Carnegie Mellon

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

Workshop on Inter Process Communication Solutions

LAB 1: C PRIMER CS444/544 WENJIN HU JAN 16TH, 2009

CSCB09: Software Tools and Systems Programming. Bianca Schroeder IC 460

Interprocess Communication E. Im

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

Lecture 5. Systems Programming: Unix Processes Creation: fork & exec Process Communication: Pipes

CS 261 Fall Mike Lam, Professor. Exceptional Control Flow and Processes

Transcription:

CSC 4304 - Systems Programming Fall 2008 Lecture - IX Process Control Tevfik Ko!ar Louisiana State University October 2 nd, 2008 1 Environment Variables $ env HOSTNAME=classes TERM=xterm-color USER=cs4304_kos HOSTTYPE=x86_64 PATH=/usr/local/bin:/usr/bin:/opt/gnome/bin:/usr/lib/ mit/sbin:./ CPU=x86_64 PWD=/classes/cs4304/cs4304_kos LANG=en_US.UTF-8 SHELL=/bin/bash HOME=/classes/cs4304/cs4304_kos MACHTYPE=x86_64-suse-linux LOGNAME=cs4304_kos... 2

Updating the Environment $ course=csc4304 $ export course $ env grep course course=csc4304 or $export course="systems programming" $ env grep course course=systems programming 3 How is Environment Implemented? 4

Example 1 #include <stdio.h> #include <malloc.h> extern char **environ; char ** ptr; for (ptr=environ; *ptr!= 0; ptr++) printf("%s\n", *ptr); 5 Example 2 #include <stdio.h> #include <malloc.h> main(int argc, char *argv[], char *env[]) char ** ptr; for (ptr=env; *ptr!= 0; ptr++) printf("%s\n", *ptr); 6

system function int system(const char *command); used to execute command strings e.g. system( date > file ); implemented using fork(), exec(), and waitpid() 7 Example 3 #include <stdio.h> #include <unistd.h> extern char **environ; char *newenv[5]; printf("the current environment is..\n"); system("env"); printf("***** Now Replacing Environment...\n"); getchar(); newenv[0] = "HOME=/on/the/range"; newenv[1] = "LOGNAME=nobody"; newenv[2] = "PATH=.:/bin:/usr/bin"; newenv[3] = "DAY=Wednesday"; newenv[4] = 0 ; environ = newenv; execlp("env", "env", NULL); 8

Getting Environment Vars char * getenv(const char *name); #include <stdio.h> #include <stdlib.h> printf("shell = %s\n", getenv("shell")); printf("host = %s\n", getenv("host")); 9 Setting Environment Vars int putenv(const char *name); //name=value int setenv(const char *name, const char *value, int rw); void unsetenv(condt char *name); #include <stdio.h> #include <stdlib.h> setenv("host", "new host name", 1); printf("host = %s\n", getenv("host")); 10

pid_t vfork(void); vfork function Similar to fork, but: child shares all memory with parent parent is suspended until the child makes an exit or exec call 11 fork example int ret, glob=10; printf("glob before fork: %d\n", glob); ret = fork(); if (ret == 0) glob++; printf("child: glob after fork: %d\n", glob) ; exit(0); if (ret > 0) if (waitpid(ret, NULL, 0)!= ret) printf("wait error!\n"); printf("parent: glob after fork: %d\n", glob) ; 12

vfork example int ret, glob=10; printf("glob before fork: %d\n", glob); ret = vfork(); if (ret == 0) glob++; printf("child: glob after fork: %d\n", glob) ; exit(0); if (ret > 0) //if (waitpid(ret, NULL, 0)!= ret) printf("wait error!\n"); printf("parent: glob after fork: %d\n", glob) ; 13 static void charatatime(char *str) char *ptr; int c; Race Conditions setbuf(stdout, NULL); for (ptr=str;c=*ptr++;) putc(c,stdout); pid_t pid; if ((pid = fork())<0) printf("fork error!\n"); else if (pid ==0) charatatime("12345678901234567890\n"); else charatatime("abcdefghijklmnopqrstuvwxyz\n"); 14

Output $ fork3 12345678901234567890 abcdefghijklmnopqrstuvwxyz $ fork3 12a3bc4d5e6f78901g23hi4567jk890 lmnopqrstuvwxyz 15 Avoid Race Conditions static void charatatime(char *str) char *ptr; int c; setbuf(stdout, NULL); for (ptr=str;c=*ptr++;) putc(c,stdout); pid_t pid; TELL_WAIT(); if ((pid = fork())<0) printf("fork error!\n"); else if (pid ==0) WAIT_PARENT(); charatatime("12345678901234567890\n"); else charatatime("abcdefghijklmnopqrstuvwxyz\n"); TELL_CHILD(); 16

Process Accounting Kernel writes an accounting record each time a process terminates acct struct defined in <sys/acct.h> 17 Process Accounting Data required for accounting record is kept in the process table Initialized when a new process is created (e.g. after fork) Written into the accounting file (binary) when the process terminates in the order of termination No records for crashed processes abnormal terminated processes 18

Pipes one-way data channel in the kernel has a reading end and a writing end e.g. who sort or ps grep ssh 19 Process Communication via Pipes int pipe(int filedes[2]); pipe creates a pair of file descriptors, pointing to a pipe inode, and places them in the array pointed to by filedes. filedes[0] is for reading filedes[1] is for writing 20

main(int ac, char *av[]) int thepipe[2], newfd, pid;*/ if ( ac!= 3 )fprintf(stderr, "usage: pipe cmd1 cmd2\n");exit(1); if (pipe(thepipe) == -1)perror( "cannot create pipe"); exit(1); if ((pid = fork()) == -1)fprintf(stderr,"cannot fork\n"); exit(1); /* * parent will read from reading end of pipe */ if ( pid > 0 ) /* the child will be av[2] */ close(thepipe[1]); /* close writing end */ close(0); /* will read from pipe */ newfd=dup(thepipe[0]); /* so duplicate the reading end */ if ( newfd!= 0 ) /* if not the new stdin.. */ fprintf(stderr,"dupe failed on reading end\n"); exit(1); close(thepipe[0]); /* stdin is duped, close pipe */ execlp( av[2], av[2], NULL); exit(1); /* oops */ 21 /* * child will write into writing end of pipe */ close(thepipe[0]); /* close reading end */ close(1); /* will write into pipe */ newfd=dup(thepipe[1]); /* so duplicate writing end */ if ( newfd!= 1 ) /* if not the new stdout.. */ fprintf(stderr,"dupe failed on writing end\n"); exit(1); close(thepipe[1]); /* stdout is duped, close pipe */ execlp( av[1], av[1], NULL); exit(1); /* oops */ 22

Summary Process Control Environment Process Accounting Pipes Hmm.. Next Class: Signals Source codes available under: /classes/cs4304/cs4304_kos/code 23 Acknowledgments Advanced Programming in the Unix Environment by R. Stevens The C Programming Language by B. Kernighan and D. Ritchie Understanding Unix/Linux Programming by B. Molay Lecture notes from B. Molay (Harvard), T. Kuo (UT- Austin), G. Pierre (Vrije), M. Matthews (SC), and B. Knicki (WPI). 24