linux fork 한 후 signal보내기..도움좀 주세요..

coollhb의 이미지

안녕하세요..

질문이 있어서 고수분들의 도움을 요청하기 위해 글을 올립니다..;;

부모프로세스가 fork를 하는데요 부모가 fork하는 수가 인자로 입력받은 문자열의 갯수입니다..

./a.out abc를 하면 부모는 3번의 fork를 하여 모두 부모의 signal를 기다리고 있다가

부모는 첫번째 생긴 자식프로세스에게 시그날을 보내고

인자로 받은 첫번째 문자를 어느 파일에 write를 하고는 2번째 생긴 프로세스에게 시그날을 보내고

첫번째 자식프로세스는 죽습니다. 그리고 시그널을 받은 2번째 자식프로세스는 1번째 프로세스와 같은

일을 하고 인자로 받은 문자열의 갯수만큼 반복한후에 프로그램이 종료하게 만드는게 목적입니다..

아직 하지 못한게 부모프로세스가 첫번째 자식에게 시그널을 보내는 부분부터 시그널을 다 보내고

종료만 되면 되는데 이부분을 잘 모르겠습니다 ㅠㅠ

그리고 지금 어떻게 돌아가고 있는지도 알려주시면 감사하겠습니다....(__)

고수님들~~ 바쁘시더라도 조금 도와주세요 ㅠㅠ

int Pid[16] = {0,};
int PPid[12];
 
int handler(int CNT, char *ARGV)
{
int check_pid;
check_pid = getpid(); 
printf("throw the signal to %d child.\n", check_pid);
 
if (Pid[CNT] == check_pid) //pid와 배열Pid를 비교한다.
{
Write_char(CNT, ARGV); //같다면 다음 함수를 호출한다.
exit(1);
}
}
 
void Write_char(int count, char *argv)
{
int mypid, fd = 0;
//char Value[3];
char Value[2];
pid_t pid;
{ 
mypid = getpid(); 
 
Value[0] = argv[count - 1]; //자꾸 가비지값이 들어가서
Value[1] = '\0'; //배열공간을 일일이 값으로
// Value[2] = '\0'; //채워넣었습니다.
write(fd, Value, strlen(Value) + 1);
 
printf("파일쓰기성공 : %s\n",Value);
exit(1);
}
else if(0 > (fd = open("file.txt", O_WRONLY | O_CREAT | O_APPEND, 0644)))
{
printf("파일열기실패\n");
exit(1);
}
}
 
int main(int argCount, char *argValue[])
{
int length, i;
 
length = strlen(argValue[1]);
pid_t pid;
int fd;
 
for(i = 0; i <= length; i++)
{
 
if(-1 == pid) // if error
{
printf("pid값이 -1입니다.종료합니다.\n");
exit(1);
 
}
else if(0 == pid) // if child
{
Pid[i] = getpid();
char get_pid[10];
sprintf(get_pid, "%d ", Pid[i]);
// int Temp = 0;
// Temp = strlen(get_pid);
 
if (0 < (fd = open("file1.txt", O_WRONLY | O_CREAT | O_APPEND, 0644)))
{
write(fd, get_pid, strlen(get_pid));
}
int temp = 0;
//int k = strlen(argValue[1]);
//char Argc[k];
 
// Pid[i] = getpid();
temp = Pid[i];
// write_char(i, temp, argValue[1]);
 
kill(temp, handler(i, argValue[1]));
 
// sleep(1);
exit(1);
 
}
else // if parent
{
printf("난 엄마다. 포크 성공\n");
pid = fork();
if(pid > 0)
{
printf("fork = %d\n", pid);
PPid[i] = pid;
printf("배열 : %d\ncount : %d\n", PPid[i], i);
waitpid(PPid[i]);
}
} 
 
 
}
return 0;
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.