当前位置:首页 » 编程语言 » c语言进程

c语言进程

发布时间: 2022-02-12 04:02:29

㈠ 用c语言编写的进程管理程序

#include<conio.h>
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<string.h>
structPCB_type
{charpid[100];//进程名输入用字符串健壮性比较好
intpriority;
intcputime;
intstate;
};//缺少“};”
intshumu=0,pid_l;
structPCB_typeneicun[20];
structPCB_typehc[10];
intmax=0;
intnumber=0;
voidcreate();
voidrun();
voidhuanchu();
voidkill();
intmain()
{
intn,a;
n=1;
system("color1d");
while(n==1)
{
system("cls");
printf(" **********************************************");
printf(" *进程演示系统*");
printf(" **********************************************");
printf(" 1.创建新的进程2.查看运行进程");
printf(" 3.换出某个进程4.杀死运行进程");//新增了一个唤醒功能
printf(" 5.退出系统");
printf(" **********************************************");
printf(" 请选择(1~5):");
scanf("%d",&a);
switch(a)
{case1:
create();
printf(" pressanykeytogoon~");
getch();
break;
case2:
run();
printf(" pressanykeytogoon~");
getch();
break;
case3:
huanchu();
printf(" pressanykeytogoon~");
getch();
break;
case4:
kill();
printf(" pressanykeytogoon~");
getch();
break;
case5:
exit(0);
default:
n=1;
break;
}
}
}
voidcreate()
{
if(shumu>=20)
{
printf(" 内存已满,请先结束或换出进程 ");
}
else
{
shumu++;
printf(" 请输入新进程的程序名 ");
scanf("%s",neicun[shumu-1].pid);
printf(" 请输入新进程的优先级(数字) ");//
scanf("%d",&neicun[shumu-1].priority);
printf(" 请输入新进程的需要的运行时间 ");//
scanf("%d",&neicun[shumu-1].cputime);
printf(" 创建进程时令其状态为就绪 ");
neicun[shumu-1].state=2;//1为等待,2就绪,3为运行

}
printf(" 创建进程成功! ");
}
voidrun()
{
if(shumu<=0)//查看//判断是否存在进程
{
printf("当前状态无进程,按任意键继续创建进程 ");
return;
}
intmax=0;
for(inti=0;i<shumu;i++)//
if((neicun[i].state==2&&neicun[i].priority>=neicun[max].priority))
{
max=i;//这里判断优先级,优先级高的进程优先执行。
}
if(neicun[max].state==2)
{
neicun[max].state=3;//进程运行,状态为3
system("color5F");
printf("/*********************当前已有进程%d个*************************/: ",shumu);
for(inti=0;i<shumu;i++){
printf("进程编号:%d",i+1);
printf(" /***********正在运行进程程序名:%s*************************/ ",neicun[i].pid);
printf(" /***********该进程的优先级:%d*****************************/ ",neicun[i].priority);
printf(" /***********该进程的需要运行时间:%d***********************/ ",neicun[i].cputime);
printf(" /***********该进程的状态:%d(1为等待,2就绪,3为运行)******/ ",neicun[i].state);}//这里增加显示当前运行的进程
}

}
/*换出*/
voidhuanchu()
{
intk;
if(shumu<=0)//判断是否存在进程
{
printf("当前进程数目为0,不能执行该操作 ");
return;
}
printf("当前已有进程%d个: ",shumu);
for(inth=0;h<shumu;h++)//当前所有的进程
printf("序号:%d 程序名:%s 优先级:%d 运行时间:%d 状态:%d "
,h,neicun[h].pid,neicun[h].priority,neicun[h].cputime,neicun[h].state);
printf("请输入要换出程序的序号:");
scanf("%d",&k);
if(neicun[k].state==3)
{
neicun[k].state=1;
printf("已被换出,进程名为:%s、状态为:[%d]",neicun[k].pid,neicun[k].state);
}
else
printf("无法换出,进程名为:%s的进程",neicun[k].pid);//换出结果提示
}
voidkill()
{

if(shumu<=0)//对存在的进程进行判断
{
printf("当前进程数目为0,不能执行该操作 ");
return;
}
intk=0;
printf("/******************当前已有进程%d个******************/: ",shumu);
for(inth=0;h<shumu;h++)//当前所有的进程
printf("序号:%d 程序名:%s 优先级:%d 运行时间:%d 状态:%d "
,h,neicun[h].pid,neicun[h].priority,neicun[h].cputime,neicun[h].state);
printf("请输入要杀死程序的序号:");
scanf("%d",&k);
neicun[k].cputime=0;
neicun[k].priority=0;
neicun[k].state=0;
if(k==(shumu-1))
shumu--;
else{
for(intj=k+1;j<shumu;j++)
{
strcmp(neicun[j-1].pid,neicun[j].pid);
neicun[j-1].priority=neicun[j].priority;
neicun[j-1].cputime=neicun[j].cputime;
neicun[j-1].state=neicun[j].state;
}
shumu--;
}
printf("进程名为:%s已被杀死! ",neicun[k].pid);//显示进程已被杀死

}

㈡ 如何仅用c语言实现查看系统的进程,和进程名

#include<stdlib.h>
#include<stdio.h>

voidmain()
{

//显示所有的进程
system("tasklist");
system("pause");

}

显示正在运行的进程

㈢ 怎么用C语言写一个关闭后台某进程的程序

//kill进程from名字
BOOL KillProcessFromName(LPCSTR lpProcessName)
{
//创建进程快照(TH32CS_SNAPPROCESS表示创建所有进程的快照)
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
//PROCESSENTRY32进程快照的结构体
PROCESSENTRY32 pe;
//实例化后使用Process32First获取第一个快照的进程前必做的初始化操作
pe.dwSize = sizeof(PROCESSENTRY32);

//下面的IF效果同:
//if(hProcessSnap == INVALID_HANDLE_VALUE) 无效的句柄
if(!Process32First(hSnapShot,&pe))
{
return FALSE;
}
CString strProcessName = lpProcessName;
//将字符串转换为小写
strProcessName.MakeLower();

//如果句柄有效 则一直获取下一个句柄循环下去
while (Process32Next(hSnapShot,&pe))
{
//pe.szExeFile获取当前进程的可执行文件名称
CString scTmp = pe.szExeFile;

//将可执行文件名称所有英文字母修改为小写
scTmp.MakeLower();
//比较当前进程的可执行文件名称和传递进来的文件名称是否相同
//相同的话Compare返回0
if(!scTmp.Compare(strProcessName))
{
//从快照进程中获取该进程的PID(即任务管理器中的PID)
DWORD dwProcessID = pe.th32ProcessID;
HANDLE hProcess = ::OpenProcess(PROCESS_TERMINATE,FALSE,dwProcessID);
::TerminateProcess(hProcess,0);
CloseHandle(hProcess);
return TRUE;
}
scTmp.ReleaseBuffer();
}
strProcessName.ReleaseBuffer();
return FALSE;
}
以杀死qq程序为例,我们可以通过下列方式:
KillProcessFromName("qq.exe");

㈣ 怎么用c语言结束某进程

首先你要知道该进程的PID


如果在windows下可以用tskill PID命令来实现,如果是c语言,只需要用system函数就可以实现

#include"stdio.h"
#include"stdlib.h"
intmain()
{
system("tskillXXXX");
return0;
}

其中XXXX就是你需要传入的进程的PID


如果是linux或者unix的系统


则用kill命令:

#include"stdio.h"
#include"stdlib.h"
intmain()
{
system("killXXXX");
return0;
}

XXX一样是进程的PID

㈤ c语言进程调度

意思就是你这个cpp中 定义的这些函数都不合法,不符合C++语言的规定。

㈥ c语言多进程编程

多进程这个词用得比较少,听过来有点不熟悉。你这个程序在linux下应该很容易实行,就是个进程间通信的问题,管道、消息队列、共享内存都可以,可以找找相关资料。昨天失言不好意思。
三个源文件分别为1.c、2.c、3.c一个头文件share.h。
share.h:
//共享的内存,两个数组
typedef struct{
int a[2];
int b[2];
int id;
}share_use;

1.c:
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>

#include"share.h"

int main(){
void *shared_memory = (void *)0;
share_use *share_stuff;
int shmid;

shmid=shmget((key_t)1234,sizeof(share_use),0666|IPC_CREAT);//创建共享内存
if(shmid==-1){
fprintf(stderr,"共享内存创建失败!\n");
exit(1);
}

shared_memory = shmat(shmid, (void *)0,0);//让进程可以访问共享内存
if(shared_memory == (void *)-1){
fprintf(stderr,"启用共享内存失败!\n)";
exit(1);
}
printf("Memory attached at %X\n",(int)shared_memory);

share_stuff = (share_use *)shared_memory;
share_stuff->id=0;
share_stuff->a[0]=1;
share_stuff->a[1]=2;
while(1){
if(share_stuff->id)
exit(0);
sleep(10);
}
}

2.c:
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>

#include"share.h"

int main(){
void *shared_memory = (void *)0;
share_use *share_stuff;
int shmid;

shmid=shmget((key_t)1234,sizeof(share_use),0666|IPC_CREAT);//创建共享内存
if(shmid==-1){
fprintf(stderr,"共享内存创建失败!\n");
exit(1);
}

shared_memory = shmat(shmid, (void *)0,0);//让进程可以访问共享内存
if(shared_memory == (void *)-1){
fprintf(stderr,"启用共享内存失败!\n");
exit(1);
}
printf("Memory attached at %X\n",(int)shared_memory);

share_stuff = (share_use *)shared_memory;
share_stuff->b[0]=share_stuff->a[0]*100;
share_stuff->b[1]=share_stuff->a[1]*100;
while(1)
{
if(share_stuff->id)
exit(0);
sleep(10);
}
}
3.c:
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>

#include"share.h"

int main(){
void *shared_memory = (void *)0;
share_use *share_stuff;
int shmid;

shmid=shmget((key_t)1234,sizeof(share_use),0666|IPC_CREAT);//创建共享内存
if(shmid==-1){
fprintf(stderr,"共享内存创建失败!\n");
exit(1);
}

shared_memory = shmat(shmid, (void *)0,0);//让进程可以访问共享内存
if(shared_memory == (void *)-1){
fprintf(stderr,"启用共享内存失败!\n");
exit(1);
}
printf("Memory attached at %X\n",(int)shared_memory);

share_stuff = (share_use *)shared_memory;
printf("共享内存中有元素:%d , %d",share_stuff->b[0],share_stuff->b[1]);
share_stuff->id=1;
return 0;
}
linux或unix环境下编译

㈦ c语言里什么叫进程

对于代码来说, 进程就是当前程序运行的载体。 也就是程序本身。
进程结束, 程序也就退出了。

所以 中止进程后, 当前程序所有代码都不会执行。

㈧ 用C语言编写有关进程的程序

你是要模拟还是要怎么地?、

㈨ C语言函数的进程函数

所在函数库为stdlib.h、process.h
void abort() 此函数通过调用具有出口代码3的_exit写一个终止信息于
stderr,并异常终止程序 无返回值
int exec…装入和运行其它程序
int execl( char *pathname,char *arg0,char *arg1,…,char *argn,NULL)
int execle( char *pathname,char *arg0,char *arg1,…,
char *argn,NULL,char *envp[])
int execlp( char *pathname,char *arg0,char *arg1,…,NULL)
int execlpe(char *pathname,char *arg0,char *arg1,…,NULL,char *envp[])
int execv( char *pathname,char *argv[])
int execve( char *pathname,char *argv[],char *envp[])
int execvp( char *pathname,char *argv[])
int execvpe(char *pathname,char *argv[],char *envp[])
exec函数族装入并运行程序pathname,并将参数
arg0(arg1,arg2,argv[],envp[])传递给子程序,出错返回-1
在exec函数族中,后缀l、v、p、e添加到exec后,
所指定的函数将具有某种操作能力
有后缀 p时,函数可以利用DOS的PATH变量查找子程序文件
l时,函数中被传递的参数个数固定
v时,函数中被传递的参数个数不固定
e时,函数传递指定参数envp,允许改变子进程的环境,
无后缀e时,子进程使用当前程序的环境
void _exit(int status)终止当前程序,但不清理现场
void exit(int status) 终止当前程序,关闭所有文件,写缓冲区的输出(等待输出),
并调用任何寄存器的出口函数,无返回值
int spawn…运行子程序
int spawnl( int mode,char *pathname,char *arg0,char *arg1,…,
char *argn,NULL)
int spawnle( int mode,char *pathname,char *arg0,char *arg1,…,
char *argn,NULL,char *envp[])
int spawnlp( int mode,char *pathname,char *arg0,char *arg1,…,
char *argn,NULL)
int spawnlpe(int mode,char *pathname,char *arg0,char *arg1,…,
char *argn,NULL,char *envp[])
int spawnv( int mode,char *pathname,char *argv[])
int spawnve( int mode,char *pathname,char *argv[],char *envp[])
int spawnvp( int mode,char *pathname,char *argv[])
int spawnvpe(int mode,char *pathname,char *argv[],char *envp[])
spawn函数族在mode模式下运行子程序pathname,并将参数
arg0(arg1,arg2,argv[],envp[])传递给子程序.出错返回-1
mode为运行模式
mode为 P_WAIT 表示在子程序运行完后返回本程序
P_NOWAIT 表示在子程序运行时同时运行本程序(不可用)
P_OVERLAY表示在本程序退出后运行子程序
在spawn函数族中,后缀l、v、p、e添加到spawn后,
所指定的函数将具有某种操作能力
有后缀 p时, 函数利用DOS的PATH查找子程序文件
l时, 函数传递的参数个数固定.
v时, 函数传递的参数个数不固定.
e时, 指定参数envp可以传递给子程序,允许改变子程序运行环境.
当无后缀e时,子程序使用本程序的环境.
int system(char *command) 将MSDOS命令command传递给DOS执行
转换子程序,函数库为math.h、stdlib.h、ctype.h、float.h
char *ecvt(double value,int ndigit,int *decpt,int *sign)
将浮点数value转换成字符串并返回该字符串
char *fcvt(double value,int ndigit,int *decpt,int *sign)
将浮点数value转换成字符串并返回该字符串
char *gcvt(double value,int ndigit,char *buf)
将数value转换成字符串并存于buf中,并返回buf的指针
char *ultoa(unsigned long value,char *string,int radix)
将无符号整型数value转换成字符串并返回该字符串,radix为转换时所用基数
char *ltoa(long value,char *string,int radix)
将长整型数value转换成字符串并返回该字符串,radix为转换时所用基数
char *itoa(int value,char *string,int radix)
将整数value转换成字符串存入string,radix为转换时所用基数
double atof(char *nptr) 将字符串nptr转换成双精度数,并返回这个数,错误返回0
int atoi(char *nptr) 将字符串nptr转换成整型数, 并返回这个数,错误返回0
long atol(char *nptr) 将字符串nptr转换成长整型数,并返回这个数,错误返回0
double strtod(char *str,char **endptr)将字符串str转换成双精度数,并返回这个数,
long strtol(char *str,char **endptr,int base)将字符串str转换成长整型数,
并返回这个数,
int toascii(int c) 返回c相应的ASCII
int tolower(int ch) 若ch是大写字母('A'-'Z')返回相应的小写字母('a'-'z')
int _tolower(int ch) 返回ch相应的小写字母('a'-'z')
int toupper(int ch) 若ch是小写字母('a'-'z')返回相应的大写字母('A'-'Z')
int _toupper(int ch) 返回ch相应的大写字母('A'-'Z')

㈩ C语言创建进程

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>

intmain()
{
for(intj=0;j<5;++j){
intpid=fork();
if(pid==0){//child
printf("j:%d,pid:%d. ",j,getpid());
exit(0);
}elseif(pid<0){//error
exit(-1);
}
}

for(inti=0;i<5;++i){
wait(NULL);
}

printf("End! ");

return0;
}

热点内容
轻应用缓存 发布:2025-05-18 00:31:02 浏览:251
鸟存储空气 发布:2025-05-18 00:20:24 浏览:201
linux刻录iso 发布:2025-05-18 00:16:15 浏览:663
php动态参数 发布:2025-05-18 00:12:05 浏览:425
安卓应用上传 发布:2025-05-18 00:11:57 浏览:803
数对的算法 发布:2025-05-18 00:11:02 浏览:382
linuxwhile 发布:2025-05-18 00:10:08 浏览:144
xpftp外网 发布:2025-05-17 23:58:11 浏览:386
如何评价一个服务器的性能 发布:2025-05-17 23:40:53 浏览:271
淘宝客适合什么服务器 发布:2025-05-17 23:39:26 浏览:614