进程

2016-12-12  本文已影响0人  萌面大叔2

1.进程的命令


查看CPU:top

查看所有的进程:ps -A

查看当前的进程:ps

关闭进程:kill -9 进程id

int main(int argc, char *argv[])

{

char caFile[64] = {'\\0'};

strncpy(caFile, argv[1], sizeof(caFile));

int ret = -1;

strcat(caFile, ".old");

ret = rename(argv[1], caFile);

if (-1 == ret)

{

printf("rename error:%s\\n", strerror(errno));

return -1;

}

int fdNew = -1;

fdNew = open(argv[1], O_WRONLY | O_CREAT

, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

if (-1 == fdNew)

{

printf("open error:%s\\n", strerror(errno));

return -1;

}

int fdOld = -1;

fdOld = open(caFile, O_RDONLY);

if (-1 == fdOld)

{

printf("open error:%s\\n", strerror(errno));

return -1;

}

off_t offset = 0;

printf("please input position:");

scanf("%ld", &offset);

char caBuf[PER_IO_BYTES] = {'\\0'};

int iLeft = offset;

//将指定位置之前的数据拷贝到新文件中

while (iLeft)

{

if (iLeft >= PER_IO_BYTES)

{

ret = read(fdOld, caBuf, PER_IO_BYTES);

}

else

{

ret = read(fdOld, caBuf, iLeft);

}

if (-1 == ret)

{

printf("read error:%s\\n", strerror(errno));

break;

}

iLeft -= ret;

ret = write(fdNew, caBuf, ret);

if (-1 == ret)

{

printf("write error:%s\\n", strerror(errno));

break;

}

}

//在指定的位置写入数据

char *pData = "$$$qwertyuiopasdfghjklzxcvbnm$$$";

ret = write(fdNew, pData, strlen(pData));

if (-1 == ret)

{

printf("write error:%s\\n", strerror(errno));

return;

}

//将指定位置之后的数据写入新文件中

while (1)

{

ret = read(fdOld, caBuf, PER_IO_BYTES);

if (-1 == ret)

{

printf("read error:%s\\n", strerror(errno));

break;

}

else if (0 == ret)

{

break;

}

ret = write(fdNew, caBuf, ret);

if (-1 == ret)

{

printf("write error:%s\\n", strerror(errno));

break;

}

}

close(fdNew);

close(fdOld);

ret = remove(caFile);

if (-1 == ret)

{

printf("remove error:%s\\n", strerror(errno));

return -1;

}

return 0;

}

上一篇 下一篇

猜你喜欢

热点阅读