advanced c programming over linux day1

Post on 13-May-2017

225 views 2 download

transcript

Mohamed Saad 1

Advanced Topics in C ProgrammingProgramming under Linux

By: Mohamed Ahmed Saad

Mohamed Saad 2

Day1 1/2

1- System and Process Information.

– Your First Linux C Program.– Command-Line Arguments.– Environment variables.– User Information.– Host Information.

Mohamed Saad 3

Day1 2/2

2- Process Control.

– Process Identifiers.– Duplicating a Process Image.– Starting New Processes.– Replacing a Process Image.

Mohamed Saad 4

Day1

1- System and Process Information.

– Your First Linux C Program.– Command-Line Arguments.– Environment variables.– User Information.– Host Information.

Mohamed Saad 5

● Your First Linux C Program (1/4).

In file hello.c

#include <stdio.h> //1

int main(int argc, char *argv[]) //2

{

printf("hello, world\n"); //3

exit(0); //4

}

System & Process Information (01/12)

Mohamed Saad 6

● Compile and Run Program:

$gcc -o hello.exe hello.c

$./hello.exe

hello, world

$

System & Process Information (02/12)

Mohamed Saad 7

● Your First Linux C Program (3/4).

#include <stdio.h> //1Included file where functions like (printf) is defined.

int main(int argc, char *argv[]) //2int : returned value of the program (represents program status, 0: succeed, otherwise: failed.

To print return result of your program $echo $?

Argc : argument count ( number of arguments passed to the program).

Argv : an array of character pointers.

First entry is a pointer to program name.

Subsequent entries point to subsequent strings on the command line.

System & Process Information (03/12)

Mohamed Saad 8

● Your First Linux C Program (4/4).

printf("hello, world\n"); //3Prints “hello, world” on the screen

exit(0); //4Indicates that the program executed successfully.

System & Process Information (04/12)

Mohamed Saad 9

Day1

1- System and Process Information.

– Your First Linux C Program.– Command-Line Arguments.– Environment variables.– User Information.– Host Information.

Mohamed Saad 10

● Command-Line Arguments (1/2):in file “echoarg.c”

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char *argv[])

{

int i;

for (i = 0; i < argc; i++)

printf("argv[%d]: %s\n", i, argv[i]);

exit(0);

}

System & Process Information (05/12)

Mohamed Saad 11

● Command-Line Arguments (2/2):$ ./echoarg arg1 TEST foo

argv[0]: ./echoarg

argv[1]: arg1

argv[2]: TEST

argv[3]: foo

$

System & Process Information (06/12)

Mohamed Saad 12

Day1

1- System and Process Information.

– Your First Linux C Program.– Command-Line Arguments.– Environment variables.– User Information.– Host Information.

Mohamed Saad 13

● Environment variables (1/4):● Environment List:

Each program is also passed an environment list. Like the argument list, the environment list is an array of character pointers, with each pointer containing the address of a null-terminated C string.

The address of the array of pointers is contained in the global variable environ:

extern char **environ;

System & Process Information (07/12)

Mohamed Saad 14

● Environment List:

environ:

environmentpointer

environment List

HOME=/home/msaad\0

PATH=:/usr/lib/mit/sbin:\0

SHELL=/bin/bash\0

USER=msaad\0

LOGNAME=msaad\0

NULL

environment String

System & Process Information (08/12)

Mohamed Saad 15

● Environment Variables (3/4):The environment strings are usually of the form

name=value

● Get Environment value:

#include <stdlib.h>

char *getenv(const char *name);

Returns: pointer to value associated with name, NULL if not found

System & Process Information (09/12)

Mohamed Saad 16

● Environment Variables (4/4):● Set Environment value:

#include <stdlib.h>

int putenv(char *str);

Returns: 0 if OK, nonzero on error.

System & Process Information (10/12)

Mohamed Saad 17

Mohamed Saad 18

Day1

1- System and Process Information.

– Your First Linux C Program.– Command-Line Arguments.– Environment variables.– User Information.– Host Information.

Mohamed Saad 19

● User Information (1/1):

Password File:● passwd File fields are ontained in a passwd structure

that is defined in <pwd.h>

#include <pwd.h>

struct passwd *getpwuid

(uid_t uid);

struct passwd *getpwnam

(const char *name);

Both return: pointer if OK,

NULL on error

System & Process Information (11/12)

Mohamed Saad 20

Day1

1- System and Process Information.

– Your First Linux C Program.– Command-Line Arguments.– Environment variables.– User Information.– Host Information.

Mohamed Saad 21

Host Information (1/1):#include <sys/utsname.h>

int uname(struct utsname *name);

Returns: 0 value if OK, -1 on error

struct utsname {

char sysname[]; /* name of the operating system */

char nodename[]; /* name of this node */

char release[]; /* current release of operating system */

char version[]; /* current version of this release */

char machine[]; /* name of hardware type */

};

System & Process Information (12/12)

Mohamed Saad 22

Mohamed Saad 23

Day1

2- Process Control.

– Process Identifiers.– Duplicating a Process Image.– Starting New Processes.– Replacing a Process Image.

Mohamed Saad 24

Process Control

● Process Identifiers (1/1):#include <unistd.h>

pid_t getpid(void);

Returns: process ID of calling process

pid_t getppid(void);

Returns: parent process ID of calling process

uid_t getuid(void);

Returns: real user ID of calling process

gid_t getgid(void);

Returns: real group ID of calling process

Process ControlProcess ControlProcess ControlProcess ControlProcess ControlProcess Control

Mohamed Saad 25

Day1

2- Process Control.

– Process Identifiers.– Duplicating a Process Image.– Starting New Processes.– Replacing a Process Image.

Mohamed Saad 26

● Duplicating a Process Image (1/5):● An existing process can create a new one by calling the

fork function.

#include <unistd.h>

pid_t fork(void);

Returns: 0 in child, process ID of child in parent, -1 on error

Process Control

Mohamed Saad 27

● Duplicating a Process Image (2/5):

Process Control

Initial Process

fork()

Returns a childProcess Id

Origin process continue

Returns Zero

Child Process

Mohamed Saad 28

● Duplicating a Process Image (3/5):● An existing process can create a new one by calling the

fork function.

#include <unistd.h>

pid_t fork(void);

Returns: 0 in child, process ID of child in parent, -1 on error

Process Control

Mohamed Saad 29

pid_t new_pid;

new_pid = fork();

switch(new_pid){

case -1: /*Error*/ break;

case 0: /*Child*/ break;

default : /*Parent*/

}

Process Control

Mohamed Saad 30

● Duplicating a Process Image (5/5):#include <sys/wait.h>

pid_t wait(int *statloc);

● Returns: process ID if OK, 0 (see later), or 1 on error.

● If stat_loc is not a null pointer, the exit status of

the child process will be written to the location to which it points

Process Control

Mohamed Saad 31

Mohamed Saad 32

Day1

2- Process Control.

– Process Identifiers.– Duplicating a Process Image.– Starting New Processes.– Replacing a Process Image.

Mohamed Saad 33

● Starting New Processes (1/1):You can cause a program to run from

inside another program.

#include <stdlib.h>

int system (const char *cmdstring);

Process Control

Mohamed Saad 34

Day1

2- Process Control.

– Process Identifiers.– Duplicating a Process Image.– Starting New Processes.– Replacing a Process Image.

Mohamed Saad 35

● Replacing a Process Image (1/2): When a process calls one of the exec functions, that process is completely replaced by the new program, and

the new program starts executing at its main function. The process ID does not change across an

exec, because a new process is not created; exec merely replaces the current process its text, data,

heap, and stack segments with a brand new program from disk.

Process Control

Mohamed Saad 36

● Replacing a Process Image (2/2):#include <unistd.h>

● int execl(const char *pathname, const char *arg0,

... /* (char *)0 */ );

● int execv(const char *pathname, char *const argv []);

Process Control

Mohamed Saad 37

Mohamed Saad 38

Mohamed Saad 39