Du ng ha m for kiê m tra xem đa process cha hay con.. Dùng hàm wait hay waitpid process cha đ trạ thái tr từ process con... Người dùng có t hạ chương trình theo hai cách: Mặt tiê n foreg
Trang 1Ba i thư c ha 3
Môn hoc : Hê điê u ha nh
Chu ky sô ng cu a process
1 Viê t chương trin
char **environ
chay chuong trinh /tenfile.c
#include <stdio.h>
#include <stdlib.h>
h in ra ca c chuô i môi trươ ng thông qua khai ba o extern
extern char **environ; // environment array
int main(int argc, char **argv)
{
/* this is an array of strings */
char ** env = environ;
/* this while loop checks each *env (the pointer is moved forward to the next memory location with *env++) it loops, until *env points to null */ while (*env) printf("%s\n",*env++); // step through environment
exit(0);
}
2 Viê t chương trin h tao 1 tâp ca c process con
Trang 2Du ng ha m for kiê m tra xem đa process cha hay con Dùng hàm wait() hay waitpid process cha đ trạ thái tr từ process con
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t fork_retval ;
printf ( "Start process : %d\n", getpid() ) ;
fork_retval = fork() ;
if ( fork_retval > 0 )
{
// parent printf ( "This is the parent : %d, starting child %d\n", getpid(), fork_retval ) ;
wait() ; }
else if ( fork_retval == 0 )
{
printf ( "This is the child : %d\n", getpid() ) ; }
else
{
printf ( "Some error occurred\n" );
}
fork_retval = fork() ;
if ( fork_retval > 0 )
{
// parent printf ( "This is the parent : %d, starting child %d\n", getpid(), fork_retval ) ;
wait() ; }
else if ( fork_retval == 0 )
{
printf ( "This is the child : %d\n", getpid() ) ; }
else
{
printf ( "Some error occurred\n" );
}
return getpid() ;
}
Trang 3Người dùng có t hạ chương trình theo hai cách: Mặt tiê n (foreground) Mặt hậu (background)
a Chạy foreground process: gõ lệnh tương ứng với tên chương trình theo cách thông thường; Tương tác đượ với người dùng qua thiết bị nhập chuẩn (standard input) là bàn phím Kết xu t cu a chương trình c yếu là màn hình (standard output) Trình thông dịch lệnh sẽ bị chặn cho tới khi chương trình kết thúc
- Châ m dư t chươ tri nh foreground: Ctrl-C
- Tạm dừng chươ tri nh: Ctrl –Z
b Chạy background process: thêm d u & (ampersand) vào cuô i dòng lệnh
Ví dụ: $find /usr/man -name *.1 –print > sect1 &
Tri nh thông dịch tạo ra một process thư c hiệ chương tri nh đồ g thời in ra job
number ([n]) và PID (Process IDentifier) cu a process được tạo ra, ngay sau đó tri nh thông dịch sẵn sàng nhận lệnh mơ i
- Process background vẫ xu t ra standard output là màn hình trong lúc thư c thi
ầ tái đị h hướng standard output đ tránh m t dữ liệu xu t
- Người dùng không th tương tác với chương trình qua standard input là bàn phím với chương trình chạy mặt hậu cầ pha i tái định hướng standard input quan file khi chương trình ần nhập dữ liệu
- Liệt kê các job đang tích cư c: dùng lệnh jobs
$jobs -l
- Dùng lệnh bg (background) đ chuy process tư foreground sang chạ background mặt hậu
- Dùng lệnh f đê chuyê n process tư background sang chay
fg job_number
foreground (Nếu chỉ có một quá trình chạ mặt hậu thì không cầ job_number)
find / -name “cron*” > output1.txt &
find / -name “*.* > output2.txt &
ls –l > output3.txt &
Trang 4jobs
fg 1
ls –l
4 Sư dun g ha m execlp
C pháp: int execlp(const char *file, const char *arg, );
Các ha m exe ẽ thay thế process gọi h bằng chương trình tương ứng trong tham
s nhập cu a h m Vu ng text, data, stack bị thay thế, vu ng user (user area) không bị thay thế
● Ha m có chứa kí t l (execl, execlp, execle) danh sách tham l
pointer đến string, kết thu c danh sách n một NULL pointer
● Ha m có chứa kí t p (execlp, execvp) ch p nhận tên cu a chương tr ầ
chạy, khi chạ ương tri nh đó, nó sẽ ti m trong execution path hiệ tại; các
h không có kí t p ph i cung c p đầ đ đường dẫ đến chương tri nh cần chạy
Thưc hiên các chươ tri nh sau:
C ươ tri nh 1
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
main() {
char *temp,*temp1,*temp2;
temp1="Funny"; temp2="world";
execlp("echo","echo",temp1,temp2,NULL);
printf("Error");
}
C ươ tri nh 2
/* Example of use of fork system call */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
main()
{
int pid;
Trang 5pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork failed!\n");
exit(-1);
}
else if (pid==0) { /* child process */
printf("I am the child, return from fork=%d\n", pid);
execlp("/bin/ps", "ps", NULL);
}
else { /* parent process */
printf("I am the parent, return from fork, child pid=%d\n", pid);
wait(NULL);
printf("Parent exiting!\n");
exit(0);
}
}
5 Ta o 1 pipe
Thông thường, process tạo 1 pipe ngay trươ c khi no fork ra 1 hay nhiê u process con Pipe đươc du đê giao tiê p giư a process cha va con hay giữa các process anh em
Ha m pipe tạo 1 pipe va đăt file descriptor c o đâ u đoc
filedes[1]
va viê t cu a pipe va o filedes[0]
C ươ tri nh sau sư dun
pipe va process co đoc
int pipe (int filedes[2])
g ha m fork đ tạo 1 process con Process cha viê t dư liêu
tư pipe
va o
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
/* Read characters from the pipe and echo them to stdout */
void
read_from_pipe (int file)
{
FILE *stream;
int c;
stream = fdopen (file, "r");
Trang 6while ((c = fgetc (stream)) != EOF)
putchar (c);
fclose (stream);
}
/* Write some random text to the pipe */
void
write_to_pipe (int file)
{
FILE *stream;
stream = fdopen (file, "w");
fprintf (stream, "hello, world!\n");
fprintf (stream, "goodbye, world!\n");
fclose (stream);
}
int
main (void)
{
pid_t pid;
int mypipe[2];
/* Create the pipe */
if (pipe (mypipe))
{
fprintf (stderr, "Pipe failed.\n");
return EXIT_FAILURE;
}
/* Create the child process */
pid = fork ();
if (pid == (pid_t) 0)
{
/* This is the child process
Close other end first */
close (mypipe[1]);
read_from_pipe (mypipe[0]);
return EXIT_SUCCESS;
}
else if (pid < (pid_t) 0)
{
/* The fork failed */
fprintf (stderr, "Fork failed.\n");
return EXIT_FAILURE;
}
Trang 7else
{
/* This is the parent process
Close other end first */
close (mypipe[0]);
write_to_pipe (mypipe[1]);
return EXIT_SUCCESS;
}
}
Them doan nay vaob chuong trinh 3
#include <stdio.h>
Void main()
{
Whiale () Printf (“acb”);
}
Chay: /test1 >b1.txt &
/test2>b2.txt &
Jobs [1]
Jobs [2]
fg1