在visualc++可以运行的程序在linux下怎么不能,Linux环境下C++只允许单个程序运行...

本程序在Ubuntu和Centos系统下能够正常使用!

具体实现的代码:

bool isSingleProcess ()

{

long pid;

char full_name[1024] = {0};

char proc_name[1024] = {0};

int fd;

pid = getpid();

cout << "pid = " << pid;

sprintf(full_name, "/proc/%ld/cmdline", pid);

if (access(full_name, F_OK) == 0)

{

fd = open (full_name, O_RDONLY);

if (fd == -1)

return false;

read (fd, proc_name, 1024);

close (fd);

}

else

return false;

char self_proc_name[512] = {0};

char * p = proc_name;

int pt = 0;

while (*p != ' ' && *p != '\0')

{

self_proc_name[pt] = *p;

p++;

pt++;

}

string self_final_name = basename(self_proc_name);

cout << "self_final_name = " << self_final_name << endl << flush;

DIR *dir;

struct dirent * result;

dir = opendir ("/proc");

while ((result = readdir (dir)) != NULL)

{

if (! strcmp(result->d_name, ".") || ! strcmp (result->d_name, "..") || ! strcmp (result->d_name, "thread-self")

|| ! strcmp (result->d_name, "self") || atol (result->d_name) == pid)

continue;

memset(full_name, 0, sizeof(full_name));

memset(proc_name, 0, sizeof(proc_name));

sprintf(full_name, "/proc/%s/cmdline", result->d_name);

if (access(full_name, F_OK) == 0)

{

fd = open (full_name, O_RDONLY);

if (fd == -1)

continue;

read (fd, proc_name, 1024);

close (fd);

char *q = proc_name;

pt = 0;

memset(self_proc_name, 0, sizeof (self_proc_name));

while (*q != ' ' && *q != '\0')

{

self_proc_name[pt] = *q;

q++;

pt++;

}

string other_final_name = basename(self_proc_name);

if (self_final_name == other_final_name)

{

cout << "full_name = " << full_name << endl << flush;

cout << "other_final_name: " << other_final_name << endl;

return true;

}

}

}

return false;

}

在main函数中的开始添加以下内容:

int main (int argc, char ** argv)

{

// judge single

if (isSingleProcess())

{

cout << "process is running in another place" << endl << flush;

return 0;

}

......

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值