[ CODE ] Process Creation + str

38A·2023년 4월 22일
1

Operating System

목록 보기
10/14
post-thumbnail

Process Creation

✔️ fork()

int fork();
In parent process, return pid of child
In child process, return zero
If fail return negative value

fork() : 전부 다 복사
fork1() : 호출한 현재 thread만 복사

✔️ exec() family

The new program substitutes(대체) the original one
declared in <unistd.h>

int execl(const char path, const char arg0, ..., const char argn, char NULL → meaning last argument);
→ l : list

int execv(const char path, char const argv[] → 마지막은 NULL);
→ v : vector

int execlp(const char file, const char arg0, ..., const char argn, char NULL);
→ p가 붙으면 Path 자동검색

int execvp(const char file, char const argv[]);
→ p가 붙으면 Path 자동검색

int result = execvp(argv[0], argv);
if(result == -1) 
	fprintf(stderr, "Error! Failed to run the command\n");

✔️ wait()

pid_t wait(int *stat_loc);

Parameter

stat_loc : an integer pointer
If stateloc == NULL, it is ignored
Otherwise: receives status information from child process
➡️ Ex
exit(code); // in child process ( code → stat_loc 전달 )

✔️ sscanf()

int sscanf(const char str, const char format, ...);
return read data #, if fail return EOF

int cnt = sscanf(expression, "%d %c %d", &operand1, &operator, &operand2);
if(cnt < 3)
	return 0;

✔️ sprintf()

int sprintf(char str, const char format, ...);
return written data #, if fail return negative #

sprintf(out_msg.content, str); // save str
sprintf(out_msg.content, "%d", res_eval); // int to str
profile
HGU - 개인 공부 기록용 블로그

0개의 댓글