Some Example C Programs. These programs show how to use the exec function.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Source: T.Y. Wong, Chinese University of Hong Kong Supplementary materials: Command shell using fork, exec, and wait.
Pointers and Arrays C and Data Structures Baojian Hua
Unix Processes operating systems. The Process ID Unix identifies each process with a unique integer called a process ID. The process that executes the.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
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.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Exec Function calls Used to begin a processes execution. Accomplished by overwriting process imaged of caller with that of called. Several flavors, use.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Project BRAIN Usually done in groups of two Each student responsible for implementing parts 1-4 with their partner. Each team must demonstrate working.
Processes: program + execution state
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.
Minishell InKwan Yu Topics Unix System calls waitpid() pipe() dup2() C function calls strtok() strcmp() Minishell Software Enginnering.
Creating and Executing Processes
Command Line Arguments plus Variable-Length Arrays Systems Programming.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
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.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Pipe-Related System Calls COS 431 University of Maine.
Operating Systems Process Creation
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
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,
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Process Management Azzam Mourad COEN 346.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
CSCI 330 UNIX and Network Programming
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
OPERATING SYSTEMS 3 - PROCESSES PIETER HARTEL 1. Principle of concurrency - giving a process the illusion that it owns the whole machine  A process has:
Shell Execution Basic: fork, child execs, parent waits code of program in box –RC == return value from fork() Call fork RC=0 Call exec Subsequent instructions.
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.
1 Homework Continue with K&R Chapter 5 –Skipping sections for now –Not covering section 5.12 Continue on HW5.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
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.
Pointers as arrays C++ Programming Technologies. Pointers vs. Arrays Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
CSCI 4061 Recitation 2 1.
Section 8: Processes What is a process Creating processes Fork-Exec
Characters and Strings
Command Line Arguments
Command line arguments
Command Line Arguments
CSC 382: Computer Security
UNIX PROCESSES.
Lecture 5: Process Creation
Command Line Arguments
Fork and Exec Unix Model
Command-line arguments
Tutorial 3 Tutorial 3.
Command Line Parameters
Process Programming Interface
Command-line arguments
Tutorial: The Programming Interface
Characters and Strings
EECE.4810/EECE.5730 Operating Systems
Intro to the Shell with Fork, Exec, Wait
System Programming: Process Management
Presentation transcript:

Some Example C Programs

These programs show how to use the exec function.

exec Function calls Used to begin a processes execution. Accomplished by overwriting process imaged of caller with that of called. Several flavors, use the one most suited to needs.

exec Function calls int execv( char *path, char *argv[]) ; path: directory path to executable image. Can be your program, or system program. argv: Array of pointers to null terminated strings. These are the arguments to the program being executed.

exec Function calls two conventions with argv: 1) argv[0] should hold the name of the program to be executed. 2) The last element must be NULL.

main (int argc, *argv[]) { int pid ; char *args[2] ; pid = fork() ; if (pid ==0) { args[0] = “./a.out” ;All executed by args[1] = NULL ;child process i = execv(“./aout”, args) ; printf(“OOOpppssss.\n”) ; } else printf(“Not a problem!\n”) ; Executed by parent }

int pid ; char *args[2] ; pid = fork() ;

int pid ; char *args[2] ; pid = ? fork Parent int pid ; char *args[2] ; pid = 0 Child

int pid ; char *args[2] ; pid = fork() if (pid == 0) {args[0] = “./a.out” ; args[1] = NULL ; i = execv(“./aout”, args) ; printf(“OOOpppssss.\n”) ; } else printf(“Not ….”);

Parent int pid ; char *args[2] ; pid = fork() if (pid == 0) {args[0] = “./a.out” ; args[1] = NULL ; i = execv(“./aout”, args) ; printf(“OOOpppssss.\n”) ; } else printf(“Not ….”);

Parent int pid ; char *args[2] ; pid = fork() if (pid == 0) {args[0] = “./a.out” ; args[1] = NULL ; i = execv(“./aout”, args) ; printf(“OOOpppssss.\n”) ; } else printf(“Not ….”); Child int pid ; char *args[2] ; pid = fork() if (pid == 0) {args[0] = “./a.out” ; args[1] = NULL ; i = execv(“./aout”, args) ; printf(“OOOpppssss.\n”) ; } else printf(“Not ….”);

Parent int pid ; char *args[2] ; pid = fork() if (pid == 0) {args[0] = “./a.out” ; args[1] = NULL ; i = execv(“./aout”, args) ; printf(“OOOpppssss.\n”) ; } else printf(“Not ….”); a.out

These slides show how to parse input from the command line.

#include main() { char *ptr ; char input[1024] ; int i ; while(1) { i = read(0, input, 512) ; //read from stdin ptr = input ; while (*ptr != '\n') { if (*ptr == ' ') printf("Space!\n") ; else printf("Read %c\n", *ptr) ; ptr++ ; }

#include main() { char *ptr ; char input[512] ; int i ; while(1) { i = read(0, input, 512) ; //read from stdin ptr = input ; In C, the name of an array is the base address of the array. Now ptr points to the first element of array input.

#include main() { char *ptr ; char input[512] ; int i ; while(1) { i = read(0, input, 512) ; //read from stdin ptr = input ; while (*ptr != '\n') ptr is the address of a character. *ptr dereferences the pointer to give the actual value at that address. T h i s i s ptr = 140 *ptr = ‘T’

{ if (*ptr == ' ') printf("Space!\n") ; else printf("Read %c\n", *ptr) ; ptr++ ; } printf statement prints string to terminal. Uses control string and variables. printf(“Read %c\n”, *ptr) ; Other control characters are %d: integer %s: string %l: long int %f: float/double EXAMPLE

These slides show how to use the fork() and exec() system calls.

Simple program to fork a new process #include main (int argc, char *argv[]) { int pid ; char *args[2] ; printf("Ready to FORK\n") ; pid = fork() ; if (pid ==0) printf("I AM THE CHILD!!\n") ; else printf("I AM THE PARENT!!!\n") ; }

Simple program to start a new process executing #include main (int argc, char *argv[]) { int pid ; char *args[2] ; printf("Ready to FORK\n") ; pid = fork() ; if (pid ==0) { printf("I AM THE CHILD!!\n") ; args[0] = "./a.out" ; args[1] = NULL ; execv("./a.out", args) ; printf("OPPSSSS\n") ; } else printf("I AM THE PARENT!!!\n") ; wait(NULL) ; }

Simple program to show how children get out of control if not monitored by the parent. #include main (int argc, char *argv[]) { int pid ; char *args[2] ; printf("Ready to FORK\n") ; pid = fork() ; if (pid ==0) { printf("I AM THE CHILD!!\n") ; args[0] = "./a.out" ; args[1] = NULL ; execv("./a.out", args) ; printf("OPPSSSS\n") ; } else printf("I AM THE PARENT!!!\n") ; //wait(NULL) ; }