예제 #1
0
CSingleCheck::CSingleCheck()
{

    m_pid = 0;
    m_old = false;


    FILE *fp;
    std::string temp;
    GetTempDir( temp );
    temp += "/single.lock";
    fp = fopen( temp.c_str(), "r" );

    if ( fp == NULL )
    {
        fp = fopen( temp.c_str(), "w" );

        if ( fp == NULL )
            return ; //some error

        fprintf( fp, "%ld", long( getpid() ) );

        fclose( fp );

        //make pipe
        if ( !MakePipe() )
        {
            DBGOUT( "error to make fifo" );
        }

    }
    else
    {   //old exists,check if prog exists
        //read pid and check pid;
        long pid = 0;
        fscanf( fp, "%ld", &pid );
        char buf[ 20 ];
        sprintf( buf, "/proc/%ld", pid );
        FILE *fp2;
        fp2 = fopen( buf, "r" );

        if ( fp2 == NULL )
        {   //prog quit;
            fclose( fp );

            Clean();

            fp = fopen( temp.c_str(), "w" );


            if ( fp == NULL )
                return ; //some error

            fprintf( fp, "%ld", ( long ) getpid() );

            fclose( fp );

            if ( !MakePipe() )
            {
                DBGOUT( "error to make fifo" );
            }
        }
        else
        {   //prog exists
            fclose( fp2 );
            fclose( fp );
            m_old = true;
            m_pid = pid;
        }
    }
}
예제 #2
0
WinNamedPipeServer::WinNamedPipeServer( char *server_name)
{
	_listeningPipe = MakePipe( server_name);
	// Error handling needed
}
예제 #3
0
파일: Assignment5.c 프로젝트: Yuwain/School
int main(int argc, char *argv[]) {
	int i,
		status,
		parent,
		child[3],
		exitStatus,
		parentToChild1[2],
		child1ToChild2[2],
		child2ToChild3[2],
		child1ToParent[2],
		parentToChild2[2],
		child2ToParent[2],
		parentToChild3[2],
		child3ToParent[2];
	char command[100],
		 token[] = "GO_AHEAD";

	/* Error handling */
	if (argc != 2) {
		fprintf(stderr, "Usage: ./a.out <file name>\n");
		exit(99);
	}

	/* Create pipes */
	MakePipe(parentToChild1);
	MakePipe(child1ToChild2);
	MakePipe(child2ToChild3);
	MakePipe(child1ToParent);
	MakePipe(parentToChild2);
	MakePipe(child2ToParent);
	MakePipe(parentToChild3);
	MakePipe(child3ToParent);

	/* Set all I/O operations to non-buffered
	   Stderr is unbuffered by default, so we 
	   can have this under the error handling */
	setvbuf(stdin, NULL, _IONBF, 0);
	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(fdopen(parentToChild1[0], "r"), NULL, _IONBF, 0);
	setvbuf(fdopen(parentToChild1[1], "w"), NULL, _IONBF, 0);
	setvbuf(fdopen(child1ToChild2[0], "r"), NULL, _IONBF, 0);
	setvbuf(fdopen(child1ToChild2[1], "w"), NULL, _IONBF, 0);
	setvbuf(fdopen(child2ToParent[0], "r"), NULL, _IONBF, 0);
	setvbuf(fdopen(child2ToParent[1], "w"), NULL, _IONBF, 0);
	setvbuf(fdopen(child1ToParent[0], "r"), NULL, _IONBF, 0);
	setvbuf(fdopen(child1ToParent[1], "w"), NULL, _IONBF, 0);
	setvbuf(fdopen(parentToChild2[0], "r"), NULL, _IONBF, 0);
	setvbuf(fdopen(parentToChild2[1], "w"), NULL, _IONBF, 0);
	setvbuf(fdopen(child2ToChild3[0], "r"), NULL, _IONBF, 0);
	setvbuf(fdopen(child2ToChild3[1], "w"), NULL, _IONBF, 0);
	setvbuf(fdopen(parentToChild3[0], "r"), NULL, _IONBF, 0);
	setvbuf(fdopen(parentToChild3[1], "w"), NULL, _IONBF, 0);
	setvbuf(fdopen(child3ToParent[0], "r"), NULL, _IONBF, 0);
	setvbuf(fdopen(child3ToParent[1], "w"), NULL, _IONBF, 0);

	/* Produces 3 children */
	for (i = 0; i < 3; ++i) {
		if ((parent = child[i] = fork()) == error) {
			fprintf(stderr, "Could not produce child #%d\n", i+1);
			exit(99);
		}

		if (!parent)
			break;
	}

	/* Send and receive tokens from children and then wait for them to finish */
	if (parent) {
		fprintf(stdout, "I am the parent of the following: %d, %d, and %d\n", child[0], child[1], child[2]);
		
		WriteToPipe(parentToChild1, token);
		ReadFromPipe(child1ToParent, command);
		WriteToPipe(parentToChild2, token);
		ReadFromPipe(child2ToParent, command);
		WriteToPipe(parentToChild3, token);
		ReadFromPipe(child3ToParent, command);

		for (i = 0; i < 3; ++i) {
			waitpid(child[i], &status, 0);
			fprintf(stdout, "From main: child, id #%d ended with status %d\n", child[i], WEXITSTATUS(status));
		}

		fprintf(stdout, "Goodbye!\n");
	} 
	else {
		/* First child */
		if (i == 0) {
			ReadFromPipe(parentToChild1, command);
			
			fprintf(stdout, "I am child #%d and my id is: %d\n", i+1, getpid());
			
			/* Copies file received from argument */
			sprintf(command, "cp %s newcopy.txt", argv[1]);
			exitStatus = system(command);
			
			char done[] = "child1_done";
			
			WriteToPipe(child1ToParent, done);
			WriteToPipe(child1ToChild2, done);

			exit(++exitStatus); 
		} 
		/* Second child */
		else if (i == 1) {
			ReadFromPipe(child1ToChild2, command);
			ReadFromPipe(parentToChild2, command);

			fprintf(stdout, "I am child #%d and my id is: %d\n", i+1, getpid());

			/* Runs ls on /bin and /sbin */
			sprintf(command, "ls /bin /sbin -lR");
			exitStatus = system(command);
			
			char done[] = "child2_done";

			WriteToPipe(child2ToChild3, done);
			WriteToPipe(child2ToParent, done);
			
			exit(++exitStatus); 
		} 
		/* Third child */
		else {
			ReadFromPipe(child2ToChild3, command);
			ReadFromPipe(parentToChild3, command);

			fprintf(stdout, "I am child #%d and my id is: %d\n", i+1, getpid());

			/* Cats file received from argument and source code */
			sprintf(command, "cat %s Assignment5.c", argv[1]);
			exitStatus = system(command);

			char done[] = "child3_done";

			WriteToPipe(child3ToParent, done);

			exit(++exitStatus); 
		}
	}
	
	return 0;
}