signal을 이용한 pingpong 게임인데요 소스좀 봐주세요..

jmonaco88의 이미지

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
 
#define BUF_SIZE    512
 
void handler_parent();
void handler_child();
 
 
char *string;
 
int
main()
{
 
    //char *string;
    pid_t pid;
 
    if ( sigset(SIGUSR1, handler_parent) == SIG_ERR ) {
        fprintf(stderr, "cannot set handler_parent for SIGUSR1\n");
        exit(1);
    }
 
    if ( sigset(SIGUSR2, handler_child) == SIG_ERR ) {
        fprintf(stderr, "cannot set handler_child for SIGUSR2\n");
        exit(1);
    }
 
    pid = fork();
 
    switch ( pid ) {
        case -1 :
        {
            printf( "fork faild \n" );
            return -1;
        }
        //child
        case 0 :
        {
            for (;;) {
                kill( getppid(), SIGUSR1);
                pause();
            }
        }
        //parent
        default :
        {
            for (;;) {
                pause();
            for (;;) {
                pause();
                kill( pid, SIGUSR2);
            }
        }
 
    }
 
    exit(0);
}
 
void handler_parent()
{
    printf("Input message: ");
    fgets(string, BUF_SIZE, stdin);
    string[ strlen(string - 1) ] = '\0';
 
    if ( strcmp(string, "ping") == 0 ) {
        strcpy(string, "pong");
    }
    else if ( strcmp(string, "pong") == 0 ) {
        strcpy(string, "ping");
    }
}
 
void handler_child()
{
    fputs(string, stdout);
}

지금 현재 오류가 Segmentation Fault 뜨는데요..

string을 전역으로 한번에 쓴다는 생각이 잘못된 것일까요??

handler 함수에서 string에 저장되있는 문자열이 필요하거든요

그런데 fork로 자식프로세스랑 부모프로세스랑 string전역 변수를 같이 갖고있어서

이런식으로 오류가 난건지;; 알고싶네요

p.s ) 제가 의도한 소스는 프로세스를 실행시켜서 부모가 server가 되고 자식이 client 개념이 되는겁니다.

자식이 부모에게 신호를 날리면 부모는 ping 또는 pong 또는 아무 메세지를 입력하라고하고, 입력받은 메세지를 비교후에 변환합니다.

그리고나서 부모가 자식에게 신호를 날려주면 자식은 메세지를 출력하는 내용을 작성하고싶은데요..

메모리 공간을 공유하는것은 아니라서 불가능한 개념인가요?? 또는 신호를 날리면서 메세지를 함께 날릴수는 없는건가요??

어떻게 보면 신호를 이용해서 ipc 통신을 하고싶었던건데... 파이프랑 공유메모리를 이용해서는 다 해봤습니다.

근데 신호를 이용해서 해볼려고하는데 잘 이해가 되질 않네요... 조언 부탁드립니다

yhsuk의 이미지

일단 string의 데이터를 저장할 메모리가 할당되어 있지 않네요(Seg fault)

데이터와 시그널을 같이 보내는 건 다음 url을 참고해 보세요 (해본적은 없음)
http://www.joinc.co.kr/modules/moniwiki/wiki.php/man/2/sigqueue

Signature :) - "여유를 갖고 행동하되 게을러지지 말자"

yhsuk의 이미지

중복삭제

Signature :) - "여유를 갖고 행동하되 게을러지지 말자"

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.