클래스내 함수포인터 관련해서 질문 올립니다.

poplike의 이미지

---ㅡmain.cpp--------------------------------------------------------------------
#include "std.h"

int main()
{
Synchro sync;

sync.Process();

exit(0);
}
-----std.h------------------------------------------------------------------
#include
using namespace std;

#include
#include

#include "synchro.h"

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

-----synchro.cpp---------------------------------------------------------------

#include "std.h"

int ntimes = 0;

void Synchro::Init()
{
ntimes = 0;

this->FunP = this->p_action;
this->FunC = this->c_action;

pact.sa_handler = FunP;
sigaction(SIGUSR1, &pact, NULL);
}

void Synchro::fatal(char *s)
{
perror(s);
exit(1);
}

void Synchro::child()
{
cact.sa_handler = FunC;
cout << "Func Adress : " << cact.sa_handler << endl;
sigaction(SIGUSR1, &cact, NULL);

ppid = getppid();

for(;;)
{
cout << "child FunC Adress : " << &cact.sa_handler << endl;
cout << "child : " << ppid << endl;
sleep(1);
kill(ppid, SIGUSR1);
pause();
}
}

void Synchro::parent()
{
for(;;)
{
cout << "parent FunP Adress : " << &pact.sa_handler << endl;
cout << "parent : " << pid << endl;
pause();
sleep(1);
kill(pid, SIGUSR1);
}
}

void Synchro::p_action(int sig)
{
cout << "Parent caught signal #" << ++ntimes << endl;
}

void Synchro::c_action(int sig)
{
cout << "Child caught signal #" << ++ntimes << endl;
}

void Synchro::Process()
{
Init();

switch(pid = fork())
{
case -1:
fatal("synchro");
break;
case 0:
child();
break;
default:
parent();
break;
}
}

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

class Synchro
{
private:
pid_t pid, ppid;
void (*FunP)(int);
void (*FunC)(int);
struct sigaction pact, cact;

private:
void Init();
void fatal(char *s);
void child();
void parent();
static void p_action(int sig);
static void c_action(int sig);
public:
void Process();
};

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

부모와 자식간의 프로세스 통신 부분 예제를 해보다가

위 소스에서 굵게 표시 된 부분

void (*FunP)(int);
void (*FunC)(int);
struct sigaction pact, cact;

이 부분의 순서에 따라서 프로그램이 실행이 되었다가 안되었다가 하더군요.

함수 포인터가 sigacion 위에 있으면 잘 됩니다.

하지만 아래와 같이

struct sigaction pact, cact;
void (*FunP)(int);
void (*FunC)(int);

sigaction 을 위로 올려 주면 한번만 시그널 통신을 하고
두번째에서 멈춰 버립니다.

아무래도 함수 포인터를 잘못 써서 이런것 같은데

gdb 에 익숙하지 못하다 보니 디버깅도 못해보겠네요...ㅠㅠ

File attachments: 
첨부파일 크기
Package icon synch.zip1.23 KB

댓글 달기

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