2008-06-24

Process programming

2008/06/24



プロセスとは?

・プログラムの実行形

・独立したアドレス空間とPC, SP, レジスタのセット

・現在使用中のNWソケットや読み書き中のファイルなどのI/Oデバイスの利用状況のセット。

・シグナルにより別プロセスにメッセージを送ることが可能



↑中のプログラム自体は単純な動作の集合に過ぎない。

単純な動作の集合から、複雑な動作になっている。







------------ exec ----------------

execlでプロセスが変わる。

このため、execlのあとにプログラムを書いても意味がない。



p = path

execle = 環境変数を指定して呼び出す。



signal

interupt



signal

required #inlude



sigaction(SICINT, &act, 0);

// just registerd



act.sa_flags = 0;



important!

act.sa_handler = ouch(function);



while(1){

printf("Hello World!\n");

sleep(1);

// once output per sec



if you press Ctrl+c, the function ouch is called.



----------------------------



(ここから突如日本語が打てなくなった笑)

pipeline is 8 bit
register 16bit
cache 32 or 16 it


"ls > aaa"

output ls data into aaa
std output change dup2


opposite cat

"cat < aaa" exchange 0 of dup2 why dup2 is important? [understand fork and exec] know system call getpid, getppid getued, geteuid,...,and so on. ps command >confirm process information running

UID = process is appeared by who

The Who is the UID

each process have unique ID

PID = unique ID of each process


[flow of program]
1, init is called by OS(root)
forced to allocate memory space
almost Linux has this init program
In config file, there are a lot of programs about init.

2, init calls login program (UID = 0)
then user should input ID and password
ex tatsuo has 515 UID

3, seeing inputed ID, shell command is called
this UID is 515

if sshd has bug, root has possibility to be stolen.
*private address has more safety than global IP.


*the programs under init are supporting init program

GID is represented by 16bit integer.


getpid, getppid, getuid, getgid have no parameter.
you can fetch each ID using functions above.


fork
implement multi process eniviroment.
copy process image when it is called.
file descriptor is also copied.

usage
pid_t fork(void);
including sys/types.h and unistd.h is required!

recognize program itself is parent or child

ex
pid_t new_pid;
new_pie = fork(); // stack is already copied,

if(new_pid) == -1){
// error
}else if(new_pid == 0){
// execution as a child process
}else{
// execution as a general process
}

* if fork return -1, it shows error.
* which is occurred earlier depends on then environment.
* this above means debug work is very hard!

copy on right makes cost less

wait call
how do we synchronize child process and parent process?
while shell command is running, is shell sequential?

wait = wait for finishing of whose process.
waitpid = wait for finishing of certain process.

required #include

wait function has 3 parameters.

ex
waitpid(child, &stat_val, 0);p
// child contains child process.
// if child process is not finished yet, wait for finishing it!
// if you forget to command "&", you get 0 instead of &stat_val, then you hold or system down.
// so, it is difficult to find out where is bug.
// c++ has exception function, but c does not such a exception.
// if using language has the exception function, you should use it!
// if possible, you should not call system call, because it makes bugs lower.
* OS is still a little weak, so you pay careful attention to write arguments!
*


wait: wait for the status of child is available by family
exit: By child, configure status code, and then notice what parent has finished!


-----------------------------------------
kill
kill -kill 1234
you have killed process having PID of 1234
signal; waiting for pressing any of keyboard and something like that.

alarm(5)
signal is occurred per 5 seconds.

act.sa_handler = ding;

clock_t times(struct tms *buf)
examine how much time program spends.

int system(const char *command)
forced to call command.
In homework, you must not use the system command!

putenv, getenv
reconfigure your envinment variables.


inter communication between processes
pipe, socket


pipe
In shell command

ps -Af|grep hoge
you can move result anywhere.

int pipe(int file_descriptor[2]);
[0];for reading
[1];for writing

return
0: successful
-1; failure


Case1
Parent <----> child

parent --- file_pipes[1] | file_pipes[0] --- child
parent --- file_pipes[0] | file_pipes[1] --- child

some source codes
int file_pipes[2];

if(pipe(file_pipes) == 0){
fork_result = fork()l
...
}

close(file_pipes[1]); = pipe
data_o = read(file_pipes[0], buffer, BUFSIZ); = file_pipes[0] yu child
// reading data put into buffer

you do not have to close pipe, but it has possibility to occur bugs!
so, you should close it ASAP.


let's consider PS -As | grep hoge (== 1 | 0)
grep is fetching data from 2nd parameter to end parameter.
Using dup2,

dup2(1, p[1])
// fetching pipeline[1]

dup2(0, p[0]);
// fetching pipeline[0]

kind of
ls > aaa
cat < aaa ex program close(0); dup(p_id[0]); // equals to "dup2(0,p_id[0]);" close(p_fd[0] close(p_fd[1] pipe added name(FIFO) who call pipe system call? you should clarify that who and who inter communicate. From Command Line mkfifo < filename>

From System Call
int mkfifo(const char * filename, mode_t mode);

* this pipe is hardly used.

pipe system can be call if only two programs are related to family-child.


setjump/longjump
#include // required

jump_buf ma; // required and should be put in global

longjmp(ma, -1) // return value is -1


key = setjmp(ma) // key == -1




EX
func()
{
longjmp
}

main(){
sigaction
setjmp
printf("prompt = ")
read(ls, -1)
fork
wait
exec
}

important!
ls > a
cat <= a ls|grep - , cntl.c

fork| | | sigaction
exec| dup2 | pipe | setjmp/longjmp
wait| | |

/bin
/usr/bin


ex. ls -l
search files from upper to bottom in directory.
if you find out it, execute it,

0 件のコメント: