Beispiel #1
0
int main (int argc, char **argv){

  	if (debug) printf("------------ Starting %s --------------\n",argv[0]);

  	configuration *conf = (configuration *)malloc(sizeof(configuration));
  	int pipe1fd[2];
  	int pipe2fd[2];

    // Handle options
  	optionsHandler(argc,argv,conf);
	if (debug) printf("Opciones: numeroDeHijos: %d\t Orden: %d\t Archivo de salida: %s",conf->numberOfChilds, conf->order, conf->outputFile);

    // Create 1st pipe
    Pipe(pipe1fd);
	if (debug) puts("Pipe 1 creado");
    
    // Create 2nd pipe
    Pipe(pipe2fd);
	if (debug) puts("Pipe 2 creado");

    // Fork
    childWork(conf, pipe1fd, pipe2fd);
  	
    //Father
    dadWork(conf, pipe1fd, pipe2fd);

  return 0;
}
Beispiel #2
0
int
main(int argc, char **argv)
{
	int		i, nloop, contpipe[2], datapipe[2];
	pid_t	childpid;

	if (argc != 4)
		err_quit("usage: bw_pipe <#loops> <#mbytes> <#bytes/write>");
	nloop = atoi(argv[1]);
	totalnbytes = atoi(argv[2]) * 1024 * 1024;
	xfersize = atoi(argv[3]);

	buf = Valloc(xfersize);
	Touch(buf, xfersize);

	Pipe(contpipe);
	Pipe(datapipe);

	if ( (childpid = Fork()) == 0) {
		writer(contpipe[0], datapipe[1]);	/* child */
		exit(0);
	}
		/* 4parent */
	Start_time();
	for (i = 0; i < nloop; i++)
		reader(contpipe[1], datapipe[0], totalnbytes);
	printf("bandwidth: %.3f MB/sec\n",
		   totalnbytes / Stop_time() * nloop);
	kill(childpid, SIGTERM);
	exit(0);
}
Beispiel #3
0
int
main(int argc, char **argv)
{
    int		i, nloop, pipe1[2], pipe2[2];
    char	c;
    pid_t	childpid;

    if (argc != 2)
        err_quit("usage: lat_pipe <#loops>");
    nloop = atoi(argv[1]);

    Pipe(pipe1);
    Pipe(pipe2);

    if ( (childpid = Fork()) == 0) {
        for ( ; ; ) {		/* child */
            if (Read(pipe1[0], &c, 1) != 1)
                err_quit("read error");
            Write(pipe2[1], &c, 1);
        }
        exit(0);
    }
    /* 4parent */
    doit(pipe2[0], pipe1[1]);

    Start_time();
    for (i = 0; i < nloop; i++)
        doit(pipe2[0], pipe1[1]);
    printf("latency: %.3f usec\n", Stop_time() / nloop);

    Kill(childpid, SIGTERM);
    exit(0);
}
Beispiel #4
0
int
main(int argc, char **argv)
{
	int		pipe1[2], pipe2[2];
	pid_t	childpid;

	Pipe(pipe1);	/* create two pipes */
	Pipe(pipe2);

	if ( (childpid = Fork()) == 0) {		
		/* child: write pipe2, read pipe1 */
		Close(pipe1[1]);
		Close(pipe2[0]);

		server(pipe1[0], pipe2[1]);
		exit(0);
	}
	/* parent , read pipe2 , write pipe1*/
	Close(pipe1[0]);
	Close(pipe2[1]);

	client(pipe2[0], pipe1[1]);

	Waitpid(childpid, NULL, 0);		/* wait for child to terminate */
	exit(0);
}
Beispiel #5
0
/* create ring of n processes */
int main(int argc, char *argv[])
{
    int nproc;
    struct ring l, r;	/* 'left' ring and 'right' ring */
    pid_t pid;
    int i, status;
    pid_t *ltp, *rtp;

    bzero(&r, sizeof(struct ring));
    bzero(&l, sizeof(struct ring));
    status = nproc = pid = 0;
    if (argc != 2 || ((nproc = atoi(argv[1])) <= 0))
        err_quit("Uasge: %s processes", argv[0]);

    fprintf(stderr, "Creating ring of %d processes\n", nproc);
    ring_init(&r);
    ring_init(&l);

    for (i = 1; i < nproc; i++) {
        Pipe(r.fd);
        Pipe(l.fd);
        if ((pid = Fork()) > 0) {
            ring_dup_out(&r);
            ring_dup_in(&l);
        } else {
            ring_dup_in(&r);
            ring_dup_out(&l);
        }
        ring_close_fd(&r);
        ring_close_fd(&l);
        if (pid > 0)
            break;
    }

    if ((rtp = calloc((size_t)nproc, sizeof(pid_t))) == NULL)
        err_sys("calloc error");
    (void)msg_all(r.in, r.out, rtp, nproc);
    if ((ltp = calloc((size_t)nproc, sizeof(pid_t))) == NULL)
        err_sys("calloc error");
    (void)msg_all(l.in, l.out, ltp, nproc);

    if (pid > 0)
        if (wait(&status) == -1)
            err_sys("wait error");
    pprintt(rtp, nproc);
    pprintt(ltp, nproc);
    free(rtp);
    free(ltp);
    return 0;
}
CPipedCMD::CPipedCMD(const char *cmd) : m_szCommand(cmd), m_ChildPID(0), m_bChEOF(false)
{
    Pipe(m_iPipeFD);
    
    m_ChildPID = Fork();
    
    if (m_ChildPID == 0) // Child
    {
        if (setsid() == -1)
            _exit(127);
        
        // Redirect to stdout
        dup2(m_iPipeFD[1], STDOUT_FILENO);
        close(m_iPipeFD[0]);
        close(m_iPipeFD[1]);
        
        execlp("/bin/sh", "sh", "-c", cmd, NULL);
        _exit(127);
    }
    else if (m_ChildPID > 0) // Parent
    {
        ::Close(m_iPipeFD[1]);
        m_PollData.fd = m_iPipeFD[0];
        m_PollData.events = POLLIN | POLLHUP;
    }
}
Beispiel #7
0
int
main(int argc, char **argv)
{
	int		i, nloop, contpipe[2], msqid;
	pid_t	childpid;

	if (argc != 4)
		err_quit("usage: bw_svmsg <#loops> <#mbytes> <#bytes/write>");
	nloop = atoi(argv[1]);
	totalnbytes = atoi(argv[2]) * 1024 * 1024;
	xfersize = atoi(argv[3]);

	buf = Valloc(xfersize);
	Touch(buf, xfersize);
	buf->mtype = 1;

	Pipe(contpipe);
	msqid = Msgget(IPC_PRIVATE, IPC_CREAT | SVMSG_MODE);

	if ( (childpid = Fork()) == 0) {
		writer(contpipe[0], msqid);		/* child */
		exit(0);
	}
	Start_time();
	for (i = 0; i < nloop; i++)
		reader(contpipe[1], msqid, totalnbytes);
	printf("bandwidth: %.3f MB/sec\n",
		   totalnbytes / Stop_time() * nloop);

	kill(childpid, SIGTERM);
	Msgctl(msqid, IPC_RMID, NULL);
	exit(0);
}
Beispiel #8
0
output_t* run(char* path, char** args, int nargs) {
    output_t* output = (output_t*) Malloc(sizeof(*output));
    int pfd[2];
    Pipe(pfd);
    pid_t pid = Fork();
    if(!pid) {
        Close(pfd[0]);
        char** argv;
        int argc = nargs + 1;
        argv = (char**) Malloc(sizeof(char*) * (argc + 1));
        for(int i = 0; i < argc; i++) {
            argv[i] = (char*) Malloc(ARG_LEN);
        }
        argv[argc] = NULL;
        strcpy(argv[0], path);
        for(int i = 1; i < argc; i++) {
            assert(args[i - 1] != NULL);
            strcpy(argv[i], args[i - 1]);
        }
        Close(STDOUT);
        dup2(pfd[1], STDOUT);
        execv(path, argv);
        perror("execv");
    }
    Close(pfd[1]);
    waitpid(pid, &output->retstat, 0);
    output->nbytes = read(pfd[0], output->out, BUF_SIZE);
    return output;
}
Beispiel #9
0
FILE *
SessionImpl::OpenBZip2File (/*[in]*/ const PathName & path)
{
    AutoBZ2 autoBz2 (BZ2_bzopen(path.Get(), "rb"));
    if (autoBz2.Get() == 0)
    {
        FATAL_MIKTEX_ERROR ("OpenBZip2File",
                            T_("BZ2_bzopen() failed for some."),
                            path.Get());
    }
    FILE * pFiles[2];
    Pipe (pFiles, PIPE_SIZE);
    try
    {
        auto_ptr<BZip2ReaderThreadArg> pArg (new BZip2ReaderThreadArg);
        pArg->bzin = autoBz2.Get();
        pArg->fileout = pFiles[1];
        auto_ptr<Thread> pThread (Thread::Start(BZip2ReaderThread, pArg.get()));
        pArg.release ();
        autoBz2.Detach ();
        return (pFiles[0]);
    }
    catch (const exception &)
    {
        if (pFiles[0] != 0)
        {
            fclose (pFiles[0]);
        }
        if (pFiles[1] != 0)
        {
            fclose (pFiles[1]);
        }
        throw;
    }
}
Beispiel #10
0
/* ring_init: initialise ring */
static void ring_init(struct ring *r)
{
    Pipe(r->fd);
    r->in = dup(r->fd[IN]);
    r->out = dup(r->fd[OUT]);
    ring_close_fd(r);
}
Beispiel #11
0
void
Pipe (/*[out]*/ FILE *	pFiles[2],
                /*[in]*/ size_t	pipeSize)
{
    int handles[2];
    Pipe (handles, pipeSize);
    pFiles[0] = 0;
    pFiles[1] = 0;
    try
    {
        pFiles[0] = FdOpen(handles[0], "rb");
        pFiles[1] = FdOpen(handles[1], "wb");
    }
    catch (const exception &)
    {
        if (pFiles[0] != 0)
        {
            fclose (pFiles[0]);
        }
        else
        {
            Close (handles[0]);
        }
        if (pFiles[1] != 0)
        {
            fclose (pFiles[1]);
        }
        else
        {
            Close (handles[1]);
        }
        throw;
    }
}
Beispiel #12
0
void BuildCommand(char **my_argv,char *buf)
{
  eatblank(&buf);
  my_argv[0]=buf;
  int index=1;
  char *p=buf;
      while(*p!='\0')
      {
        if(*p==' ')
        {
          *p='\0';
          p++;
          eatblank(&p) ;
          if(*p!='|')
          {
            my_argv[index++]=p;
          }
          continue;
        }
        else if(*p=='|')
        {
          p++;
          //p++;
          my_argv[index]=NULL;
          Pipe(my_argv,p);
        }
        else
        {
          p++;
        }
      }
     my_argv[index]=NULL;
}
void loop_ttl ()
{
  int ttl, done;

  Pipe (pipefd);     /* the pipe for the alarm handler */

  recvfd = socket (sasend->sa_family, SOCK_RAW, IPPROTO_ICMP);
  if (recvfd == -1) {
    if (errno == EPERM) {
      printf ("\nclink was unable to open a raw socket.  The most\n");
      printf ("likely cause is that you are not running it as root.\n");
      exit (1);
    } else {
      err_sys ("opening raw socket in clink");
    }
  }

  fcntl (recvfd, F_SETFL, O_NONBLOCK);
  setuid (getuid ());

  sendfd = socket (sasend->sa_family, SOCK_DGRAM, 0);

  sabind->sa_family = sasend->sa_family;
  sport = (getpid() & 0xffff) | 0x8000;       /* source UDP port # */
  sock_set_port (sabind, salen, htons(sport));
  Bind (sendfd, sabind, salen);

  Signal (SIGALRM, sig_alrm);

  for (ttl = 1; ttl <= max_ttl; ttl++) {
    done = send_probes (ttl);
    if (done > 0) break;
  }
}
Beispiel #14
0
int main(int argc, char **argv) {
    int i;
    int read_fd, write_fd;
    int read_bytes, written_bytes, pipe_retval;
    char buf[256];

    Print("calling pipe");
    pipe_retval = Pipe(&read_fd, &write_fd);
    assert(pipe_retval == 0);

    for(i = 0; i < 5; i++) {
        written_bytes = Write(write_fd, "beep", 4);
        Print("written %d bytes\n", read_bytes);
        assert(written_bytes == 4);
        read_bytes = Read(read_fd, buf, 256);
        Print("read %d bytes\n", read_bytes);
        assert(read_bytes == 4);
        assert(strncmp(buf, "beep", 4) == 0);
    }

    Close(write_fd);

    read_bytes = Read(read_fd, buf, 256);
    assert(read_bytes == 0);

    Close(read_fd);

    written_bytes = Write(write_fd, "beep", 4);
    assert(written_bytes <= -1);
    read_bytes = Read(read_fd, buf, 256);
    assert(read_bytes <= -1);

    return 0;
}
/* explore behaviour of select when write end of pipe is closed */
int main(void)
{
	int fd[2];
	pid_t pid;
	fd_set rset;
	int maxfd;
	int nready, nbytes;
	char buf[BUFSIZ];
	
	Pipe(fd);

	if (signal(SIGPIPE, sig_pipe) == SIG_ERR)
		err_sys("signal error");

	if ((pid = Fork()) == 0) { /* child */
		Close(fd[IN]);
		sleep(2);	/* let parent run first */
		/* write(fd[OUT], "pipe write!", 11); */
		Close(fd[OUT]);	/* close the write end */
	} else {		/* parent */
		Close(fd[OUT]);
		maxfd = fd[IN];
		for ( ; ; ) {
			FD_ZERO(&rset);
			FD_SET(fd[IN], &rset);
			FD_SET(STDIN_FILENO, &rset);
			if ((nready = select(maxfd+1, &rset, NULL, NULL, NULL)) < 0)
				err_sys("select error");
			fprintf(stderr, "Select returned: %d\n", nready);
			if (FD_ISSET(fd[IN], &rset)) {
				if ((nbytes = read(fd[IN], buf, BUFSIZ)) == 0) {
					err_msg("read got 0 bytes");
					break;
				}
				fprintf(stderr, "read from pipe nbytes: %d\n", nbytes);
				buf[nbytes] = '\0';
				if (nbytes == EOF)
					err_msg("pipe read got EOF");
				fprintf(stderr, "read from pipe: %s\n", buf);
			}
			if (FD_ISSET(STDIN_FILENO, &rset)) {
				if ((nbytes = read(STDIN_FILENO, buf, BUFSIZ)) < 0)
					err_sys("read error");
				buf[nbytes] = '\0';
				fprintf(stderr, "read from stdin: %s\n", buf);
			}
		}
		
	}
	sleep(2);
	exit(0);
}
Beispiel #16
0
void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
    int n, maxfdp1;
	const int on = 1;
	char sendline[MAXLINE], recvline[MAXLINE+1];
	fd_set rset;
	socklen_t len;
	struct sockaddr *preply_addr;

	preply_addr = (struct sockaddr *)Malloc(servlen);

	Setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));

	Pipe(pipefd);
	maxfdp1 = max(sockfd, pipefd[0]) + 1;
	FD_ZERO(&rset);

	Signal(SIGALRM, recvfrom_alarm);

	while (Fgets(sendline, MAXLINE, fp) != NULL)
	{
		Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
		
		alarm(5);
		for ( ; ; )
		{
			FD_SET(sockfd, &rset);
			FD_SET(pipefd[0], &rset);
			if ((n = select(maxfdp1, &rset, NULL, NULL, NULL)) < 0)
			{
				if (errno = EINTR)
					continue;
				else
					err_sys("select error");
			}
			if (FD_ISSET(sockfd, &rset))
			{
				len = servlen;
				n = Recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);
				recvline[n] = 0;
				printf("from %s: %s", (char *)Sock_ntop_host(preply_addr, len), recvline);
			}
			if (FD_ISSET(pipefd[0], &rset))
			{
				Read(pipefd[0], &n, 1); /* time out */
				break;
			}
		}
	}
	free(preply_addr);
}
Beispiel #17
0
/* process an unnamed bidirectional "pipe" or "fifo" or "echo" argument with
   options */
static int xioopen_fifo_unnamed(xiofile_t *sock, struct opt *opts) {
   struct opt *opts2;
   int filedes[2];
   int numleft;
   int result;

   if (applyopts_single(&sock->stream, opts, PH_INIT) < 0)  return -1;
   applyopts(-1, opts, PH_INIT);

   if (Pipe(filedes) != 0) {
      Error2("pipe(%p): %s", filedes, strerror(errno));
      return -1;
   }
   /*0 Info2("pipe({%d,%d})", filedes[0], filedes[1]);*/

   sock->common.tag               = XIO_TAG_RDWR;
   sock->stream.dtype             = XIODATA_PIPE;
   sock->stream.fd                = filedes[0];
   sock->stream.para.bipipe.fdout = filedes[1];
   applyopts_cloexec(sock->stream.fd,                opts);
   applyopts_cloexec(sock->stream.para.bipipe.fdout, opts);

   /* one-time and input-direction options, no second application */
   retropt_bool(opts, OPT_IGNOREEOF, &sock->stream.ignoreeof);

   /* here we copy opts! */
   if ((opts2 = copyopts(opts, GROUP_FIFO)) == NULL) {
      return STAT_NORETRY;
   }

   /* apply options to first FD */
   if ((result = applyopts(sock->stream.fd, opts, PH_ALL)) < 0) {
      return result;
   }
   if ((result = applyopts_single(&sock->stream, opts, PH_ALL)) < 0) {
      return result;
   }

   /* apply options to second FD */
   if ((result = applyopts(sock->stream.para.bipipe.fdout, opts2, PH_ALL)) < 0)
   {
      return result;
   }

   if ((numleft = leftopts(opts)) > 0) {
      Error1("%d option(s) could not be used", numleft);
      showleft(opts);
   }
   Notice("writing to and reading from unnamed pipe");
   return 0;
}
Beispiel #18
0
Pipe
createPipe(const char *file, unsigned int line) {
	int fds[2];
	FileDescriptor p[2];

	if (syscalls::pipe(fds) == -1) {
		int e = errno;
		throw SystemException("Cannot create a pipe", e);
	} else {
		p[0].assign(fds[0], file, line);
		p[1].assign(fds[1], file, line);
		return Pipe(p[0], p[1]);
	}
}
Beispiel #19
0
Pipe
createPipe() {
	int fds[2];
	FileDescriptor p[2];
	
	if (syscalls::pipe(fds) == -1) {
		int e = errno;
		throw SystemException("Cannot create a pipe", e);
	} else {
		p[0] = fds[0];
		p[1] = fds[1];
		return Pipe(p[0], p[1]);
	}
}
Beispiel #20
0
int main()
{
	
	pid_t childpid;	
	int pipe1[2],pipe2[2];
	Pipe(pipe1);
	Pipe(pipe2);
	

	if((childpid=fork())==0)
	{
		close(pipe1[1]);		
		close(pipe2[0]);	
		server(pipe1[0],pipe2[1]);			
	}
	
	close(pipe1[0]);
	close(pipe2[1]);
	
	client(pipe2[0],pipe1[1]);	
	waitpid(childpid,NULL,0);
	return 0;
}
Beispiel #21
0
/* create ring of n processes */
int main(int argc, char *argv[])
{
	int nproc;
	int fd[2];
	pid_t pid;
	int i, status;
	
	status = nproc = pid = 0;
	fd[0] = fd[1] = 0;
	if (argc != 2 || ((nproc = atoi(argv[1])) <= 0))
		err_quit("Uasge: %s processes", argv[0]);

	fprintf(stderr, "Creating ring of %d processes\n", nproc+1);
	Pipe(fd);
	Dup2(fd[0], STDIN_FILENO);
	Dup2(fd[1], STDOUT_FILENO);
	Close(fd[0]);
	Close(fd[1]);
	
	for (i = 0; i < nproc; i++) {
		Pipe(fd);
		if ((pid = Fork()) > 0)
			Dup2(fd[1], STDOUT_FILENO);
		else
			Dup2(fd[0], STDIN_FILENO);
		Close(fd[0]);
		Close(fd[1]);
		if (pid > 0)
			break;
	}
	if (pid > 0)
		if (wait(&status) == -1)
			err_sys("wait error");
	fprintf(stderr, "This is process: %d, ID: %ld parent: %ld\n",
	       i , (long)getpid(), (long)getppid());
	return 0;
}
Beispiel #22
0
int main(int argc, char **argv) {
    int i, j;
    int read_fd, write_fd;
    int read_bytes, written_bytes, pipe_retval;
    char buf[1024];

    for(i = 0; i < 1024; i++) {
        buf[i] = i;
    }

    /* Print("calling pipe"); */
    pipe_retval = Pipe(&read_fd, &write_fd);
    assert(pipe_retval == 0);

    for(i = 0; i < 10; i++) {
        written_bytes = Write(write_fd, buf, 1024);
        assert(written_bytes == 1024);
    }

    Close(write_fd);

    for(i = 0; i < 10; i++) {
        read_bytes = Read(read_fd, buf, 1024);
        if(read_bytes != 1024) {
            Print("read unexpected number of bytes iteration %d rc %d\n",
                  i, read_bytes);
            assert(read_bytes == 1024);
        }
        for(j = 0; j < 1024; j++) {
            if(buf[j] != (char)j) {
                Print("mismatch at iter %d index %d, buf[j] = %d\n", i, j,
                      buf[j]);
            }
            assert(buf[j] == (char)j);
        }
    }

    read_bytes = Read(read_fd, buf, 256);
    if(read_bytes != 0) {
        Print("FAIL: expected 0, got %d reading at the end\n",
              read_bytes);
        assert(read_bytes == EWOULDBLOCK);
    }

    Close(read_fd);

    return 0;
}
Beispiel #23
0
void UEventsManager::_createPipe(
		const UEventsSender * sender,
		const UEventsHandler * receiver,
		const std::string & eventName)
{
	pipesMutex_.lock();
	bool exist = false;
	for(std::list<Pipe>::iterator iter=pipes_.begin(); iter!= pipes_.end();++iter)
	{
		if(iter->sender_ == sender && iter->receiver_ == receiver && iter->eventName_.compare(eventName) == 0)
		{
			exist = true;
			break;
		}
	}

	if(!exist)
	{
		bool handlerFound = false;
		handlersMutex_.lock();
		for(std::list<UEventsHandler*>::iterator iter=handlers_.begin(); iter!=handlers_.end(); ++iter)
		{
			if(*iter == receiver)
			{
				handlerFound = true;
				break;
			}
		}
		handlersMutex_.unlock();
		if(handlerFound)
		{
			pipes_.push_back(Pipe(sender, receiver, eventName));
		}
		else
		{
			UERROR("Cannot create the pipe because the receiver is not yet "
				   "added to UEventsManager's handlers list.");
		}
	}
	else
	{
		UWARN("Pipe between sender %p and receiver %p with event %s was already created.",
				sender, receiver, eventName.c_str());
	}
	pipesMutex_.unlock();
}
Beispiel #24
0
int
main(int argc, char **argv)
{
    int		i, nloop, doorfd, contpipe[2];
    char	c;
    pid_t	childpid;
    door_arg_t	arg;

    if (argc != 3)
        err_quit("usage: lat_door <pathname> <#loops>");
    nloop = atoi(argv[2]);

    unlink(argv[1]);
    Close(Open(argv[1], O_CREAT | O_EXCL | O_RDWR, FILE_MODE));
    Pipe(contpipe);

    if ( (childpid = Fork()) == 0) {
        doorfd = Door_create(server, NULL, 0);
        Fattach(doorfd, argv[1]);
        Write(contpipe[1], &c, 1);

        for ( ; ; )		/* child = server */
            pause();
        exit(0);
    }
    arg.data_ptr = &c;	/* parent = client */
    arg.data_size = sizeof(char);
    arg.desc_ptr = NULL;
    arg.desc_num = 0;
    arg.rbuf = &c;
    arg.rsize = sizeof(char);

    if (Read(contpipe[0], &c, 1) != 1)	/* wait for child to create */
        err_quit("pipe read error");
    doorfd = Open(argv[1], O_RDWR);
    Door_call(doorfd, &arg);	/* once to start everything */

    Start_time();
    for (i = 0; i < nloop; i++)
        Door_call(doorfd, &arg);
    printf("latency: %.3f usec\n", Stop_time() / nloop);

    Kill(childpid, SIGTERM);
    unlink(argv[1]);
    exit(0);
}
Beispiel #25
0
// Takes a command and executes it using popen,
// reads from stdout and returns the value
std::string GetOutputAndExec(std::string Cmd)
{
    std::shared_ptr<FILE> Pipe(popen(Cmd.c_str(), "r"), pclose);
    if (!Pipe)
        return "ERROR";

    char Buffer[128];
    std::string Result = "";

    while (!feof(Pipe.get()))
    {
        if (fgets(Buffer, 128, Pipe.get()) != NULL)
            Result += Buffer;
    }

    return Result;
}
Beispiel #26
0
/* $$.bp$$ */
int
main(int argc, char **argv)
{
	int		nfds;
	char	c;
	fd_set	rset;
	mqd_t	mqd;
	void	*buff;
	ssize_t	n;
	struct mq_attr	attr;
	struct sigevent	sigev;

	if (argc != 2)
		err_quit("usage: mqnotifysig5 <name>");

		/* 4open queue, get attributes, allocate read buffer */
	mqd = Mq_open(argv[1], O_RDONLY | O_NONBLOCK);
	Mq_getattr(mqd, &attr);
	buff = Malloc(attr.mq_msgsize);

	Pipe(pipefd);

		/* 4establish signal handler, enable notification */
	Signal(SIGUSR1, sig_usr1);
	sigev.sigev_notify = SIGEV_SIGNAL;
	sigev.sigev_signo = SIGUSR1;
	Mq_notify(mqd, &sigev);

	FD_ZERO(&rset);
	for ( ; ; ) {
		FD_SET(pipefd[0], &rset);
		nfds = Select(pipefd[0] + 1, &rset, NULL, NULL, NULL);

		if (FD_ISSET(pipefd[0], &rset)) {
			Read(pipefd[0], &c, 1);
			Mq_notify(mqd, &sigev);			/* reregister first */
			while ( (n = mq_receive(mqd, buff, attr.mq_msgsize, NULL)) >= 0) {
				printf("read %ld bytes\n", (long) n);
			}
			if (errno != EAGAIN)
				err_sys("mq_receive error");
		}
	}
	exit(0);
}
Beispiel #27
0
/* $begin get_dynamic */
void get_dynamic(int fd, char *filename, char *cgiargs) 
{
    char buf[MAXLINE], *emptylist[] = { NULL },httpsbuf[MAXLINE];
    int p[2];

    /* Return first part of HTTP response */
    sprintf(buf, "HTTP/1.0 200 OK\r\n");
    sprintf(buf, "%sServer: Tiny Web Server\r\n",buf);

    #ifdef HTTPS 
    if(ishttps)
    	SSL_write(ssl,buf,strlen(buf));
    else
    #endif
       	Rio_writen(fd, buf, strlen(buf));
	
    #ifdef HTTPS 
    if(ishttps)
    {
    	Pipe(p);
       	if (Fork() == 0)
	{  /* child  */ 
		Close(p[0]);
		setenv("QUERY_STRING", cgiargs, 1); 
		Dup2(p[1], STDOUT_FILENO);         /* Redirect stdout to p[1] */
		Execve(filename, emptylist, environ); /* Run CGI program */	
	}
	Close(p[1]);
	Read(p[0],httpsbuf,MAXLINE);   /* parent read from p[0] */
	SSL_write(ssl,httpsbuf,strlen(httpsbuf));
    }
    else
    #endif
    {
	if (Fork() == 0) 
	{ /* child */
		/* Real server would set all CGI vars here */
		setenv("QUERY_STRING", cgiargs, 1); 
		Dup2(fd, STDOUT_FILENO);         /* Redirect stdout to client */
		Execve(filename, emptylist, environ); /* Run CGI program */
	}
    }
    //Wait(NULL); /* Parent waits for and reaps child */
}
Beispiel #28
0
/* get pipesize */
int main(void)
{
	int fd[2];
	int cnt;

	cnt = 0;
	Pipe(fd);
	set_fl(fd[1], O_NONBLOCK);

	fprintf(stderr, "PIPE_BUF: %d\n", PIPE_BUF);
	errno = 0;
	while(write(fd[1], "a", 1) != -1)
		cnt++;
	if (errno == EAGAIN)
		fprintf(stderr, "call would block (EAGAIN). cnt: %d)\n",cnt);
	else
		fprintf(stderr, "got another error: %d %s\n", errno, strerror(errno));
	return 0;
}
Beispiel #29
0
int
main(int argc, char **argv)
{
	int		fd = 0, stat, nconflicts;
	long	i, j, nproc;
	sem_t	*ptr;
	pid_t	pid;
	ssize_t	n;

	if (argc != 2)
		err_quit("usage: locksvsemrace1 <#processes>");
	nproc = atol(argv[1]);
	Pipe(pipefd);

	ptr = My_shm(sizeof(sem_t));	/* create memory-based semaphore */
	Sem_init(ptr, 1, 0);

	for (j = 0; j < nproc; j++) {
		if (Fork() == 0) {
				/* 4child */
			Sem_wait(ptr);		/* wait for parent to start children */
			for (i = 0; i < 10; i++) {
				my_lock(fd);		/* lock the file */
				my_unlock(fd);		/* unlock the file */
			}
			exit(0);
		}
		/* parent loops around, creating next child */
	}
	for (j = 0; j < nproc; j++)
		Sem_post(ptr);	/* start all the children */

	/* now just wait for all the children to finish */
	while ( (pid = waitpid(-1, &stat, WNOHANG)) > 0)
		;
	Close(pipefd[1]);
	nconflicts = 0;
	while ( (n = Read(pipefd[0], &stat, 1)) > 0)
		nconflicts += n;
	printf("%d conflicts\n", nconflicts);
	exit(0);
}
Beispiel #30
0
int
main(int argc, char **argv)
{
	int		fd[2], n;
	char	c;
	pid_t	childpid;

	Pipe(fd);		/* assumes a full-duplex pipe (e.g., SVR4) */
	if ( (childpid = Fork()) == 0) {		/* child */
		sleep(3);
		if ( (n = Read(fd[0], &c, 1)) != 1)
			err_quit("child: read returned %d", n);
		printf("child read %c\n", c);
		Write(fd[0], "c", 1);
		exit(0);
	}
		/* 4parent */
	Write(fd[1], "p", 1);
	if ( (n = Read(fd[1], &c, 1)) != 1)
		err_quit("parent: read returned %d", n);
	printf("parent read %c\n", c);
	exit(0);
}