小编far*_*ane的帖子

popen2()在c中如何工作?

我尝试使用管道、叉子和 dup 在我的程序中执行 md5sume 命令。我发现总和代码运行成功,但我无法理解某些代码行。这是我的代码:

int infp, outfp;

char buf[128];

if (popen2("md5sum", &infp, &outfp) <= 0)

  {

    printf("Unable to exec sort\n");

    exit(1);

  }

write(infp, "hello\n", 2);

close(infp);

*buf = '\0';

read(outfp, buf, 128);

printf("buf = '%s'\n", buf);

return 0;

}
Run Code Online (Sandbox Code Playgroud)
int p_stdin[2], p_stdout[2];

pid_t pid;

if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0)

  return -1;

pid = fork();

if (pid < 0)

  return pid;

if (pid == 0)

  {

    close(p_stdin[WRITE]);

    dup2(p_stdin[READ], READ);

    close(p_stdout[READ]);

    dup2(p_stdout[WRITE], WRITE);

    execl("/bin/sh", "sh", "-c", command, NULL); …
Run Code Online (Sandbox Code Playgroud)

c fork pipe dup2

5
推荐指数
1
解决办法
3851
查看次数

为什么perl在我的数据库有空的时候放空?

我正在研究一个项目,我想将一些数据插入到数据库中.我创建了数据库和表,但我遇到了一些问题.

我决定写一个测试程序,这是我的程序代码:

#!/us/bin/perl                                                                                                                                
use strict;
use warnings;
use Data::Dumper;
use DBI;

my $localtime = localtime(time);
print $localtime,"\n",;
my $char_data;
open(my $fh, "<", "/root/testfile/1453800452_5.117.219.107.bin");

while (<$fh>){
    $char_data .= $_;
}
print $char_data,"\n",;
my @Record = unpack('C*',$char_data);
my @IMEI = splice @Record,0,17;

my $IMEI = pack('C*',@IMEI);
  #print$IMEI,"\n";                                                                                                                             
my $dbh = DBI->connect("DBI:Pg:dbname=test;host=localhost","postgres", "", {PrintError=>0,RaiseError=>1});
my $sth=$dbh->prepare(qq/insert into testtime(time,data,imei) values(?,?,?)/);
$sth->execute($localtime,$char_data,$IMEI);
Run Code Online (Sandbox Code Playgroud)

这是我的数据库:

time -----> timestamp without time zone

data------->text

imei-------->varchar           
Run Code Online (Sandbox Code Playgroud)

这是我打开的文件的内容:

^ @ ^ O356307043839678 ^ @ ^ @ ^ @ ^ @ ^ …

postgresql perl

5
推荐指数
1
解决办法
124
查看次数

Perl中的消息队列

我想用Perl实现一个消息队列。我从stdin获取数据并将其发送到队列。

我邮件的结构是

struct message => {
    mtype        => '$',
    buffer_size  => '$',
    last_message => '$',
    buff         => '$',
};
Run Code Online (Sandbox Code Playgroud)

我必须使用C程序从队列中接收数据。我的C程序以前运行良好,但是现在当我运行它以从队列中接收数据时,它显示出类似以下内容

age=HASH(0x1daa088) 1936942445      4000        
Run Code Online (Sandbox Code Playgroud)

我使用在stdout上打印的4000字节缓冲区读取数据块。但是age=HASH(0x1daa088) 1936942445程序应该打印收到的消息的大小,而不是打印出来。

这里发生了什么?是因为C中的消息是结构,而Perl中的消息是哈希吗?

我的C代码:

age=HASH(0x1daa088) 1936942445      4000        
Run Code Online (Sandbox Code Playgroud)

我的Perl代码:

#include <stdio.h>
#include <stdlib.h>
#include <linux/ipc.h>
#include <linux/msg.h>
#include <time.h>
#include <string.h>

#define bufsize 4000

struct mymsgbuf {
    long    mtype;          /* Message type */
    int     buffer_size;
    char    buff[bufsize];
    int     last_message;
} msg;

int read_message( int qid, long type, struct mymsgbuf *qbuf ) {

    int result, length;

    /* …
Run Code Online (Sandbox Code Playgroud)

c perl ipc message-queue

3
推荐指数
1
解决办法
826
查看次数

标签 统计

c ×2

perl ×2

dup2 ×1

fork ×1

ipc ×1

message-queue ×1

pipe ×1

postgresql ×1