Interacting with Unix. Getting the Process ID u Synopsis #include pid_t getpid(void); u Example: #include int main(){ pid_t n = getpid(); printf("Process.

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

Chapter 9 Times and Timers Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
System Files and Process Environment Password file Group file System identification Time Process environment.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
1 Homework / Exam HW7 due class 25 Exam 3 - class 26 –Open Book, Open Notes –Covers up through end of K&R 7 –and Appendix B Standard Library –Plus UNIX.
CPSC 451 Editors and Systems Calls1 Minix editors Mined - (mined) is a simple screen editor. Elle - (elle) is a clone of Emacs. Elvis - (elvis, ex, vi)
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
CS Lecture 15 Outline Process Management System calls – exec() – chdir() – system() – nice() – Accessing User and Group IDs – Redirection Lecture.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
CSE 451 Section 4 Project 2 Design Considerations.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Week 8 - Friday.  What did we talk about last time?  String to int conversions  Users and groups  Password files.
Some Example C Programs. These programs show how to use the exec function.
The Programming Interface. Main Points Creating and managing processes – fork, exec, wait Performing I/O – open, read, write, close Communicating between.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
Slide 1 Interprocess communication. Slide 2 1. Pipes  A form of interprocess communication Between processes that have a common ancestor  Typical use:
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
CS162B: Forking Jacob T. Chan. Fork  Forks are:  Implement with two or more prongs that is used for taking up or digging  Division into branches or.
Cli/Serv.: procs/51 Client/Server Distributed Systems v Objectives –look at how to program UNIX processes , Semester 1, Processes.
1 Homework / Exam HW7 is due next class Starting Glass chapter 4 and parts of 7 Exam 3 – Class 26 –Open Book / Open Notes –Up through End of K&R Chapter.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Creating and Executing Processes
System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.
Pipe-Related System Calls COS 431 University of Maine.
Operating Systems Process Creation
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Timers and Clocks.
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Process Management Azzam Mourad COEN 346.
1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
CSCI 330 UNIX and Network Programming
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
S -1 Library Functions. S -2 Standard Libraries Any system call is not part of the C language definition Such system calls are defined in libraries, identified.
2.1 Processes  process = abstraction of a running program  multiprogramming = CPU switches from running program to running program  pseudoparallelism.
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Week 9 - Wednesday.  What did we talk about last time?  structs.
Advanced Programming in the UNIX Environment Hop Lee.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
Computer Science 516 Addressing Modes. Addressing modes are how our programs get to data Multiple addressing modes created for specific uses Function.
Linux/UNIX Programming APUE (Process Control) 문양세 강원대학교 IT 대학 컴퓨터과학전공.
Error handling I/O Man pages
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo
Week 9 - Wednesday CS222.
Real Time Programming Tech.
CSC 382: Computer Security
Lecture 5: Process Creation
Project1: Unix Shell using Multi-Processing
Fork and Exec Unix Model
Tutorial 3 Tutorial 3.
Process Programming Interface
Tutorial: The Programming Interface
Lecture 6: Multiprogramming and Context Switching
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

Interacting with Unix

Getting the Process ID u Synopsis #include pid_t getpid(void); u Example: #include int main(){ pid_t n = getpid(); printf("Process id is %d\n", n); }

Getting and Changing the Current Directory u SYNOPSIS #include char *getcwd(char *buf, size_t size); int chdir(const char *path);

Example #include int main(){ char str[1000]; char*p=getcwd(str,1000); if(p!=str){ printf("Could not get cwd!"); exit(1); } printf("cwd is %s\n", str); chdir("/usr/bin"); printf("cwd is now %s\n",getcwd(str,1000)); }

Getting the Current System Time (1) u There are a number of library functions relating to time in C. Their prototypes are found in. u Two data types are the most important for those functions: –time_t /* Typically same as long. It is the number of seconds since epoch: 00:00:00 UTC, January 1, 1970 */ –struct tm /* See next slide. */ u Can go the microsecond or nanosecond accuracy with other structures and functions.

Getting the Current System Time (2) u struct tm contains time information broken down: struct tm{ int tm_sec; // seconds [0,61] int tm_min; // minutes [0,59] int tm_hour; // hour [0,23] int tm_mday; // day of month [1,31] int tm_mon; // month of year [0,11] int tm_year; // years since 1900 int tm_wday; // day of week [0,6] (Sunday = 0) int tm_yday; // day of year [0,365] int tm_isdst; // daylight savings flag }

Getting the Current System Time (3) u Most of the time, you only need the following two functions, but there are others: #include time_t time(time_t * time); struct tm *localtime(const time_t * time);

An Example and a Question #include int main(){ time_t t = time(NULL); struct tm * p = localtime(&t); if( p->tm_year >= 102 ){ printf("Trial version expired!\n"); exit(0); } return 0; /* Question: why don’t we free(p)? */ }

The Answer u localtime() looks like the following: struct tm * localtime(const time_t * time){ static struct tm t; t.tm_year = ……; …… return & t; } u Suggestion: Use man localtime or look up a manual page to find out the exact behavior of a function.

Calling a Command from a C Program u In a C program, we can invoke a subshell and let it run a Unix command using the system() function: #include int system(const char *); u Example: #include int main() { int k; printf("Files in Directory are: \n"); k = system("ls -l"); printf("%d is returned.\n", k); return k; }

Piping to and from Other Programs (1) u A command executed by the system() function uses the same standard input and output as the calling program. u Sometimes, we want to pipe output from the calling program to the new command, or pipe input from the new command to the calling program. u This can be done using the popen() function: #include FILE *popen(const char *command, const char *mode); int pclose(FILE *fp); u If mode is "r", popen() returns a file pointer that can be used to read the standard output of command. u If mode is "w", popen() returns a file pointer that can be used to write to the standard input of command. u popen() returns NULL on error.

Piping to and from Other Programs (2) #include int main() { FILE *fp; char buffer[100]; if ((fp = popen("ls -l", "r")) != NULL) { while(fgets(buffer, 100, fp) != NULL) { printf("Line from ls:\n"); printf(" %s\n", buffer); } pclose(fp); } return 0; }

execl (1) u The system() function returns control to the program it was called from. –Immediately, if you background the command with an &. –When the command completes, otherwise. u Occasionally, you do not want to get the control back. –For example, when your program is a loader of another program. u execl() is suitable for such purposes. It loads the new program and uses it to replace the current process.

execl (2) u Synopsis #include int execl(const char *path, const char *arg0,..., const char *argn, char * /*NULL*/); u path is the pathname of the executable file. u arg0 should be the same as path or the filename. u arg1 to argn are the actual arguments u The last parameter must be NULL (or 0).

Example #include int main() { printf("Files in Directory are:\n"); execl("/bin/ls", "ls”, "-l", NULL); printf("This line should not be printed out!\n"); return 0; } u All statements after execl() will not be executed.

Multi-process Programming u With a Unix system, you can write programs that run several processes in parallel. u For example, a web-server can invoke child processes, each of which responses to the requests from a different web-browser. u We will not get into the detail of this (see CS305a/b). But, we tell you the first step of multi-process programming, so you know where to start.

The fork() Function (1) u Synopsis #include pid_t fork() u The fork() function creates a new process. The new process (child process) is an exact copy of the calling process (parent process). u The only difference between the child and parent processes is the return value of fork(). –Child process gets 0 if fork is successful. –Parent gets process id of child or -1 on errors. u You can do different things depending on whether it is a child or a parent process.

The fork() Function (2) #include int main(){ int pid; /* Process identifier */ pid = fork(); if ( pid < 0 ) { printf("Cannot fork!!\n"); exit(1); } else if ( pid == 0 ) { /* Child process */ } else { /* Parent process */.... }