advanced c programming over linux day1
-
Upload
mohammed-sad -
Category
Documents
-
view
225 -
download
2
Transcript of advanced c programming over linux day1
![Page 1: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/1.jpg)
Mohamed Saad 1
Advanced Topics in C ProgrammingProgramming under Linux
By: Mohamed Ahmed Saad
![Page 2: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/2.jpg)
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.
![Page 3: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/3.jpg)
Mohamed Saad 3
Day1 2/2
2- Process Control.
– Process Identifiers.– Duplicating a Process Image.– Starting New Processes.– Replacing a Process Image.
![Page 4: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/4.jpg)
Mohamed Saad 4
Day1
1- System and Process Information.
– Your First Linux C Program.– Command-Line Arguments.– Environment variables.– User Information.– Host Information.
![Page 5: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/5.jpg)
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)
![Page 6: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/6.jpg)
Mohamed Saad 6
● Compile and Run Program:
$gcc -o hello.exe hello.c
$./hello.exe
hello, world
$
System & Process Information (02/12)
![Page 7: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/7.jpg)
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)
![Page 8: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/8.jpg)
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)
![Page 9: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/9.jpg)
Mohamed Saad 9
Day1
1- System and Process Information.
– Your First Linux C Program.– Command-Line Arguments.– Environment variables.– User Information.– Host Information.
![Page 10: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/10.jpg)
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)
![Page 11: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/11.jpg)
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)
![Page 12: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/12.jpg)
Mohamed Saad 12
Day1
1- System and Process Information.
– Your First Linux C Program.– Command-Line Arguments.– Environment variables.– User Information.– Host Information.
![Page 13: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/13.jpg)
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)
![Page 14: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/14.jpg)
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)
![Page 15: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/15.jpg)
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)
![Page 16: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/16.jpg)
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)
![Page 17: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/17.jpg)
Mohamed Saad 17
![Page 18: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/18.jpg)
Mohamed Saad 18
Day1
1- System and Process Information.
– Your First Linux C Program.– Command-Line Arguments.– Environment variables.– User Information.– Host Information.
![Page 19: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/19.jpg)
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)
![Page 20: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/20.jpg)
Mohamed Saad 20
Day1
1- System and Process Information.
– Your First Linux C Program.– Command-Line Arguments.– Environment variables.– User Information.– Host Information.
![Page 21: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/21.jpg)
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)
![Page 22: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/22.jpg)
Mohamed Saad 22
![Page 23: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/23.jpg)
Mohamed Saad 23
Day1
2- Process Control.
– Process Identifiers.– Duplicating a Process Image.– Starting New Processes.– Replacing a Process Image.
![Page 24: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/24.jpg)
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
![Page 25: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/25.jpg)
Mohamed Saad 25
Day1
2- Process Control.
– Process Identifiers.– Duplicating a Process Image.– Starting New Processes.– Replacing a Process Image.
![Page 26: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/26.jpg)
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
![Page 27: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/27.jpg)
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
![Page 28: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/28.jpg)
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
![Page 29: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/29.jpg)
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
![Page 30: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/30.jpg)
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
![Page 31: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/31.jpg)
Mohamed Saad 31
![Page 32: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/32.jpg)
Mohamed Saad 32
Day1
2- Process Control.
– Process Identifiers.– Duplicating a Process Image.– Starting New Processes.– Replacing a Process Image.
![Page 33: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/33.jpg)
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
![Page 34: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/34.jpg)
Mohamed Saad 34
Day1
2- Process Control.
– Process Identifiers.– Duplicating a Process Image.– Starting New Processes.– Replacing a Process Image.
![Page 35: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/35.jpg)
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
![Page 36: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/36.jpg)
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
![Page 37: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/37.jpg)
Mohamed Saad 37
![Page 38: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/38.jpg)
Mohamed Saad 38
![Page 39: advanced c programming over linux day1](https://reader034.fdocuments.us/reader034/viewer/2022051521/577cce391a28ab9e788d991b/html5/thumbnails/39.jpg)
Mohamed Saad 39