Bai thưc ha
3
Môn hoc: Hê điêu hanh
C đ : quan l tiên trinh (process)
Chu ky sông cua process
1. Viêt chương trin h in ra cac chuôi môi trương thông qua khai bao extern
char **environ
chay chuong trinh ./tenfile.c
#include <stdio.h>
#include <stdlib.h>
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 cac process con.
Bai thuc hanh he dieu hanh sô 3 – GV Phi Loan
SinhVienZone.com
1
/>
Dung ham 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() ;
}
3. Qua n l process
Bai thuc hanh he dieu hanh sô 3 – GV Phi Loan
SinhVienZone.com
2
/>
Người dùng có t
hậu (background)
hạ chương trình theo hai cách: Mặt tiên (foreground)
Mặt
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 cua 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ươ trinh foreground: Ctrl-C
- Tạm dừng chươ trinh: 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 &
Trinh thông dịch tạo ra một process thưc hiệ chương trinh đồ g thời in ra job
number ([n]) và PID (Process IDentifier) cua process được tạo ra, ngay sau đó
trinh 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ầ phai 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 foreground
fg job_number
(Nếu chỉ có một quá trình chạ mặt hậu thì không cầ job_number)
Thưc hanh cac lênh sau:
find / -name “cron*” > output1.txt &
find / -name “*.* > output2.txt &
ls –l > output3.txt &
Bai thuc hanh he dieu hanh sô 3 – GV Phi Loan
SinhVienZone.com
3
/>
jobs
fg 1
ls –l
4. Sư dun g ha m execlp
C pháp:
int execlp(const char *file, const char *arg, ...);
Các ham exe ẽ thay thế process gọi h bằng chương trình tương ứng trong tham
s nhập cua h m. Vung text, data, stack bị thay thế, vung user (user area) không bị
thay thế.
● Ham có chứa kí t l (execl, execlp, execle)
danh sách tham l
pointer đến string, kết thuc danh sách n
một NULL pointer.
● Ham có chứa kí t p (execlp, execvp) ch p nhận tên cua chương tr
ầ
chạy, khi chạ
ương trinh đó, nó sẽ tim 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 trinh cần
chạy.
Thưc hiên các chươ
C ươ
trinh sau:
trinh 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 ươ
trinh 2
/* Example of use of fork system call */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
main()
{
int pid;
Bai thuc hanh he dieu hanh sô 3 – GV Phi Loan
SinhVienZone.com
4
/>
pid = 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. Tao 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.
Ham pipe tạo 1 pipe va đăt file descriptor c o đâu đoc va viêt cua pipe vao filedes[0]
filedes[1]
int pipe (int filedes[2])
C ươ trinh sau sư dun g ham fork đ tạo 1 process con. Process cha viêt dư liêu vao
pipe va process co đoc tư pipe.
#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");
Bai thuc hanh he dieu hanh sô 3 – GV Phi Loan
SinhVienZone.com
5
/>
while ((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;
}
Bai thuc hanh he dieu hanh sô 3 – GV Phi Loan
SinhVienZone.com
6
/>
else
{
/* 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
Bai thuc hanh he dieu hanh sô 3 – GV Phi Loan
SinhVienZone.com
7
/>