Linux 进程控制(C语言)

Linux 进程控制(C语言)
Linux 进程控制(C语言)

#include

/* Clone the calling process, but without copying the whole address space.

The calling process is suspended until the new process exits or is

replaced by a call to `execve'. Return -1 for errors, 0 to the new process, and the process ID of the new process to the old process. */

extern pid_t vfork (void) __THROW;

/* Clone the calling process, creating an exact copy.

Return -1 for errors, 0 to the new process,

and the process ID of the new process to the old process. */

extern pid_t fork (void) __THROW;

/* Get the process ID of the calling process. */

extern __pid_t getpid (void) __THROW;

/* Get the process ID of the calling process's parent. */

extern __pid_t getppid (void) __THROW;

/* Set the process group ID of the calling process to its own PID.

This is exactly the same as `setpgid (0, 0)'. */

extern int setpgrp (void) __THROW;

/* Get the process group ID of the calling process.

This function is di?erent on old BSD. */

#ifndef __FAVOR_BSD

extern __pid_t getpgrp (void) __THROW;

#else

# ifdef __REDIRECT_NTH

extern __pid_t __REDIRECT_NTH (getpgrp, (__pid_t __pid), __getpgid);

# else

# de?ne getpgrp __getpgid

# endif

#endif

/* Terminate program execution with the low-order 8 bits of STATUS. */ extern void _exit (int __status) __attribute__ ((__noreturn__));

/* Replace the current process, executing PATH with arguments ARGV and

environment ENVP. ARGV and ENVP are terminated by NULL pointers. */ extern int execve (__const char *__path, char *__const __argv[],

char *__const __envp[]) __THROW __nonnull ((1));

/* Execute PATH with arguments ARGV and environment from `environ'. */ extern int execv (__const char *__path, char *__const __argv[])

__THROW __nonnull ((1));

/* Execute PATH with all arguments after PATH until a NULL pointer,

and the argument after that for environment. */

extern int execle (__const char *__path, __const char *__arg, …)

__THROW __nonnull ((1));

/* Execute PATH with all arguments after PATH until

a NULL pointer and environment from `environ'. */

extern int execl (__const char *__path, __const char *__arg, …)

__THROW __nonnull ((1));

/* Execute FILE, searching in the `PATH' environment variable if it contains

no slashes, with arguments ARGV and environment from `environ'. */ extern int execvp (__const char *__?le, char *__const __argv[])

__THROW __nonnull ((1));

/* Execute FILE, searching in the `PATH' environment variable if

it contains no slashes, with all arguments after FILE until a

NULL pointer and environment from `environ'. */

extern int execlp (__const char *__?le, __const char *__arg, …)

__THROW __nonnull ((1));

/* exec

l:the number of params is unclear, the last by NULL

v:a char array pointer point to argv list, mostly like l, use NULL means last p:autocheck the program in environment path

e:a char array pointer for environment Val. restore in envp*/

/* Get the real user ID of the calling process. */

extern __uid_t getuid (void) __THROW;

/* Get the e?ective user ID of the calling process. */

extern __uid_t geteuid (void) __THROW;

/* Get the real group ID of the calling process. */

extern __gid_t getgid (void) __THROW;

/* Get the e?ective group ID of the calling process. */

extern __gid_t getegid (void) __THROW;

/* Set the user ID of the calling process to UID.

If the calling process is the super-user, set the real

and e?ective user IDs, and the saved set-user-ID to UID;

if not, the e?ective user ID is set to UID. */

extern int setuid (__uid_t __uid) __THROW;

#if de?ned __USE_BSD || de?ned __USE_XOPEN_EXTENDED

/* Set the real user ID of the calling process to RUID,

and the e?ective user ID of the calling process to EUID. */ extern int setreuid (__uid_t __ruid, __uid_t __euid) __THROW;

#endif

#if de?ned __USE_BSD || de?ned __USE_XOPEN2K

/* Set the e?ective user ID of the calling process to UID. */

extern int seteuid (__uid_t __uid) __THROW;

#endif /* Use BSD. */

/* Set the group ID of the calling process to GID.

If the calling process is the super-user, set the real

and e?ective group IDs, and the saved set-group-ID to GID;

if not, the e?ective group ID is set to GID. */

extern int setgid (__gid_t __gid) __THROW;

#if de?ned __USE_BSD || de?ned __USE_XOPEN_EXTENDED

/* Set the real group ID of the calling process to RGID,

and the e?ective group ID of the calling process to EGID. */ extern int setregid (__gid_t __rgid, __gid_t __egid) __THROW;

#endif

#if de?ned __USE_BSD || de?ned __USE_XOPEN2K

/* Set the e?ective group ID of the calling process to GID. */ extern int setegid (__gid_t __gid) __THROW;

#endif /* Use BSD. */

/* Add INC to priority of the current process. */

extern int nice (int __inc) __THROW __wur;

#include

/* Wait for a child to die. When one does, put its status in *STAT_LOC and return its process ID. For errors, return (pid_t) -1.

This function is a cancellation point and therefore not marked with

__THROW. */

extern __pid_t wait (__WAIT_STATUS __stat_loc);

/* Wait for a child matching PID to die.

If PID is greater than 0, match any process whose process ID is PID.

If PID is (pid_t) -1, match any process.

If PID is (pid_t) 0, match any process with the

same process group as the current process.

If PID is less than -1, match any process whose

process group is the absolute value of PID.

If the WNOHANG bit is set in OPTIONS, and that child

is not already dead, return (pid_t) 0. If successful,

return PID and store the dead child's status in STAT_LOC.

Return (pid_t) -1 for errors. If the WUNTRACED bit is

set in OPTIONS, return status for stopped children; otherwise don't.

This function is a cancellation point and therefore not marked with

__THROW. */

extern __pid_t waitpid (__pid_t __pid, int *__status, int __options);

/*WIFEXITED(status): normally ?nished, return true

WIFSIGNALED(status): ?nished by signal, return true; by exit, return false

WIFSTOPPED(): waitpid use WUNTRACED, and subprocess returnd, this true

WSTOPSIG(): when WIFSTOPPED() is true, get ?nished SIGNAL

WTERMSIG(): when WIFSIGNALED() is true, get ?nished SIGNAL

WEXITSTATUS(): when WIFEXITED() is true, its usable of returning quit code*/ #include

/* Return the highest priority of any process speci?ed by WHICH and WHO

(see above); if WHO is zero, the current process, process group, or user

(as speci?ed by WHO) is used. A lower priority number means higher

priority. Priorities range from PRIO_MIN to PRIO_MAX (above). */

//which: PRIO_PROCESS, PRIO_PGRP, PRIO_USER

//who: the process

extern int getpriority (__priority_which_t __which, id_t __who) __THROW;

/* Set the priority of all processes speci?ed by WHICH and WHO (see above)

to PRIO. Returns 0 on success, -1 on errors. */

//prio is from -20 to 20

extern int setpriority (__priority_which_t __which, id_t __who, int __prio)

__THROW;

#include

/* Clone current process. */

//?ags:CLONE_VM, _FS, _FILES, _PID, SIGHAND

extern int clone (int (*__fn) (void *__arg), void *__child_stack,

int __?ags, void *__arg, …) __THROW;

#include

/* Call all functions registered with `atexit' and `on_exit',

in the reverse of the order in which they were registered,

perform stdio cleanup, and terminate program execution with STATUS. */ extern void exit (int __status) __THROW __attribute__ ((__noreturn__));

/* Register a function to be called when `exit' is called. */

extern int atexit (void (*__func) (void)) __THROW __nonnull ((1));

/* Register a function to be called with the status

given to `exit' and the given argument. */

extern int on_exit (void (*__func) (int __status, void *__arg), void *__arg) __THROW __nonnull ((1));

/* Abort execution and generate a core-dump. */

extern void abort (void) __THROW __attribute__ ((__noreturn__));

/* Set NAME to VALUE in the environment.

If REPLACE is nonzero, overwrite an existing value. */

extern int setenv (__const char *__name, __const char *__value, int __replace) __THROW __nonnull ((2));

/* Remove the variable NAME from the environment. */

extern int unsetenv (__const char *__name) __THROW;

/* Execute the given line as a shell command.

This function is a cancellation point and therefore not marked with

__THROW. */

extern int system (__const char *__command) __wur;

#include

/* void assert (int expression);

If NDEBUG is de?ned, do nothing.

If not, and EXPRESSION is zero, print an error message and abort. */

#include

/* Open FILE and return a new ?le descriptor for it, or -1 on error.

OFLAG determines the type of access used. If O_CREAT is on OFLAG,

the third argument is taken as a `mode_t', the mode of the created ?le.

This function is a cancellation point and therefore not marked with

__THROW. */

#ifndef __USE_FILE_OFFSET64

extern int open (__const char *__?le, int __o?ag, …) __nonnull ((1));

#else

# ifdef __REDIRECT

extern int __REDIRECT (open, (__const char *__?le, int __o?ag, …), open64)

__nonnull ((1));

#include

/* Return a string describing the meaning of the `errno' code in ERRNUM. */ extern char *strerror (int __errnum) __THROW;

#include

/* Set scheduling algorithm and/or parameters for a process. */

extern int sched_setscheduler (__pid_t __pid, int __policy,

__const struct sched_param *__param) __THROW;

/*SCHED_OTHER: default policy

SCHED_FIFO: FIFO means the stack, before _OTHER, after hige policy

SCHED_RR: before _OTHER, but when equal, aftered*/

/* Retrieve scheduling algorithm for a particular purpose. */

extern int sched_getscheduler (__pid_t __pid) __THROW;

#include

/* Create a new thread, starting with execution of START-ROUTINE

getting passed ARG. Creation attributed come from ATTR. The new

handle is stored in *NEWTHREAD. */

extern int pthread_create (pthread_t *__restrict __newthread,

__const pthread_attr_t *__restrict __attr,

void *(*__start_routine) (void *),

void *__restrict __arg) __THROW __nonnull ((1, 3));

/* Terminate calling thread.

The registered cleanup handlers are called via exception handling

so we cannot mark this function with __THROW.*/

extern void pthread_exit (void *__retval) __attribute__ ((__noreturn__));

/* Make calling thread wait for termination of the thread TH. The

exit status of the thread is stored in *THREAD_RETURN, if THREAD_RETURN is not NULL.

This function is a cancellation point and therefore not marked with

__THROW. */

extern int pthread_join (pthread_t __th, void **__thread_return);

/* Thread attribute handling. */

/* Initialize thread attribute *ATTR with default attributes

(detachstate is PTHREAD_JOINABLE, scheduling policy is SCHED_OTHER,

no user-provided stack). */

extern int pthread_attr_init (pthread_attr_t *__attr) __THROW __nonnull ((1)); /* Destroy thread attribute *ATTR. */

extern int pthread_attr_destroy (pthread_attr_t *__attr)

__THROW __nonnull ((1));

/* Get detach state attribute. */

//default: PTHREAD_CREATE_JONIABLE - _DETACHED unconnectalbe

extern int pthread_attr_getdetachstate (__const pthread_attr_t *__attr,

int *__detachstate)

__THROW __nonnull ((1, 2));

/* Set detach state attribute. */

extern int pthread_attr_setdetachstate (pthread_attr_t *__attr,

int __detachstate)

__THROW __nonnull ((1));

/* Get the size of the guard area created for stack over?ow protection. */ extern int pthread_attr_getguardsize (__const pthread_attr_t *__attr,

size_t *__guardsize)

/* Set the size of the guard area created for stack over?ow protection. */ extern int pthread_attr_setguardsize (pthread_attr_t *__attr,

size_t __guardsize)

__THROW __nonnull ((1));

/* Return in *PARAM the scheduling parameters of *ATTR. */

extern int pthread_attr_getschedparam (__const pthread_attr_t *__restrict

__attr,

struct sched_param *__restrict __param)

__THROW __nonnull ((1, 2));

/* Set scheduling parameters (priority, etc) in *ATTR according to PARAM. */ extern int pthread_attr_setschedparam (pthread_attr_t *__restrict __attr,

__const struct sched_param *__restrict

__param) __THROW __nonnull ((1, 2));

/* Return in *POLICY the scheduling policy of *ATTR. */

//default: SCHED_OTHER - _RR & _FIFO

extern int pthread_attr_getschedpolicy (__const pthread_attr_t *__restrict

__attr, int *__restrict __policy)

__THROW __nonnull ((1, 2));

/* Set scheduling policy in *ATTR according to POLICY. */

extern int pthread_attr_setschedpolicy (pthread_attr_t *__attr, int __policy)

__THROW __nonnull ((1));

/* Return in *INHERIT the scheduling inheritance mode of *ATTR. */

//default: PTHREAD_EXPLICIT_SCHED : by schedpolicy and params con?g

// PTHREAD_INHERIT_SCHED: inherit its parent

extern int pthread_attr_getinheritsched (__const pthread_attr_t *__restrict

__attr, int *__restrict __inherit)

__THROW __nonnull ((1, 2));

/* Set scheduling inheritance mode in *ATTR according to INHERIT. */ extern int pthread_attr_setinheritsched (pthread_attr_t *__attr,

int __inherit)

__THROW __nonnull ((1));

/* Return in *SCOPE the scheduling contention scope of *ATTR. */

//default: PTHREAD_SCOPE_SYSTEM: every pthread occurpied per system time

//the PTHREAD_SCOPE_PROCESS seemed not usable in Linux~

extern int pthread_attr_getscope (__const pthread_attr_t *__restrict __attr,

int *__restrict __scope)

/* Set scheduling contention scope in *ATTR according to SCOPE. */ extern int pthread_attr_setscope (pthread_attr_t *__attr, int __scope)

__THROW __nonnull ((1));

/* Return the previously set address for the stack. */

extern int pthread_attr_getstackaddr (__const pthread_attr_t *__restrict

__attr, void **__restrict __stackaddr)

__THROW __nonnull ((1, 2)) __attribute_deprecated__;

/* Set the starting address of the stack of the thread to be created.

Depending on whether the stack grows up or down the value must either be higher or lower than all the address in the memory block. The

minimal size of the block must be PTHREAD_STACK_MIN. */

extern int pthread_attr_setstackaddr (pthread_attr_t *__attr,

void *__stackaddr)

__THROW __nonnull ((1)) __attribute_deprecated__;

/* Return the currently used minimal stack size. */

extern int pthread_attr_getstacksize (__const pthread_attr_t *__restrict

__attr, size_t *__restrict __stacksize)

__THROW __nonnull ((1, 2));

/* Add information about the minimum stack size needed for the thread

to be started. This size must never be less than PTHREAD_STACK_MIN and must also not exceed the system limits. */

extern int pthread_attr_setstacksize (pthread_attr_t *__attr,

size_t __stacksize)

__THROW __nonnull ((1));

#ifdef __USE_XOPEN2K

/* Return the previously set address for the stack. */

extern int pthread_attr_getstack (__const pthread_attr_t *__restrict __attr,

void **__restrict __stackaddr,

size_t *__restrict __stacksize)

__THROW __nonnull ((1, 2, 3));

/* The following two interfaces are intended to replace the last two. They require setting the address as well as the size since only setting the

address will make the implementation on some architectures impossible. */ extern int pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,

size_t __stacksize) __THROW __nonnull ((1));

#endif

#ifdef __USE_GNU

/* Thread created with attribute ATTR will be limited to run only on

the processors represented in CPUSET. */

extern int pthread_attr_seta?nity_np (pthread_attr_t *__attr,

size_t __cpusetsize,

__const cpu_set_t *__cpuset)

__THROW __nonnull ((1, 3));

/* Get bit set in CPUSET representing the processors threads created with

ATTR can run on. */

extern int pthread_attr_geta?nity_np (__const pthread_attr_t *__attr,

size_t __cpusetsize,

cpu_set_t *__cpuset)

__THROW __nonnull ((1, 3));

/* Initialize thread attribute *ATTR with attributes corresponding to the

already running thread TH. It shall be called on uninitialized ATTR

and destroyed with pthread_attr_destroy when no longer needed. */

extern int pthread_getattr_np (pthread_t __th, pthread_attr_t *__attr)

__THROW __nonnull ((2));

#endif

/* Functions for scheduling control. */

/* Set the scheduling parameters for TARGET_THREAD according to POLICY

and *PARAM. */

extern int pthread_setschedparam (pthread_t __target_thread, int __policy,

__const struct sched_param *__param)

__THROW __nonnull ((3));

/* Return in *POLICY and *PARAM the scheduling parameters for TARGET_THREAD. */

extern int pthread_getschedparam (pthread_t __target_thread,

int *__restrict __policy,

struct sched_param *__restrict __param)

__THROW __nonnull ((2, 3));

/* Functions for handling conditional variables. */

/* Initialize condition variable COND using attributes ATTR, or use

the default values if later is NULL. */

extern int pthread_cond_init (pthread_cond_t *__restrict __cond,

__const pthread_condattr_t *__restrict

__cond_attr) __THROW __nonnull ((1));

/* Destroy condition variable COND. */

extern int pthread_cond_destroy (pthread_cond_t *__cond)

__THROW __nonnull ((1));

/* Wake up one thread waiting for condition variable COND. */

extern int pthread_cond_signal (pthread_cond_t *__cond)

__THROW __nonnull ((1));

/* Wake up all threads waiting for condition variables COND. */

extern int pthread_cond_broadcast (pthread_cond_t *__cond)

__THROW __nonnull ((1));

/* Wait for condition variable COND to be signaled or broadcast.

MUTEX is assumed to be locked before.

This function is a cancellation point and therefore not marked with

__THROW. */

extern int pthread_cond_wait (pthread_cond_t *__restrict __cond,

pthread_mutex_t *__restrict __mutex)

__nonnull ((1, 2));

/* Wait for condition variable COND to be signaled or broadcast until

ABSTIME. MUTEX is assumed to be locked before. ABSTIME is an

absolute time speci?cation; zero is the beginning of the epoch

(00:00:00 GMT, January 1, 1970).

This function is a cancellation point and therefore not marked with

__THROW. */

extern int pthread_cond_timedwait (pthread_cond_t *__restrict __cond,

pthread_mutex_t *__restrict __mutex,

__const struct timespec *__restrict

__abstime) __nonnull ((1, 2, 3));

/* Functions for handling cancellation.

Note that these functions are explicitly not marked to not throw an

exception in C++ code. If cancellation is implemented by unwinding

this is necessary to have the compiler generate the unwind information. */

/* Set cancelability state of current thread to STATE, returning old

state in *OLDSTATE if OLDSTATE is not NULL. */

extern int pthread_setcancelstate (int __state, int *__oldstate);

/* Set cancellation state of current thread to TYPE, returning the old

type in *OLDTYPE if OLDTYPE is not NULL. */

extern int pthread_setcanceltype (int __type, int *__oldtype);

/* Cancel THREAD immediately or at the next possibility. */

extern int pthread_cancel (pthread_t __th);

/* Test for pending cancellation for the current thread and terminate

the thread as per pthread_exit(PTHREAD_CANCELED) if it has been

cancelled. */

extern void pthread_testcancel (void);

/* Mutex handling. */

/* Initialize a mutex. */

extern int pthread_mutex_init (pthread_mutex_t *__mutex,

__const pthread_mutexattr_t *__mutexattr)

__THROW __nonnull ((1));

/* Destroy a mutex. */

extern int pthread_mutex_destroy (pthread_mutex_t *__mutex)

__THROW __nonnull ((1));

/* Try locking a mutex. */

extern int pthread_mutex_trylock (pthread_mutex_t *__mutex)

__THROW __nonnull ((1));

/* Lock a mutex. */

extern int pthread_mutex_lock (pthread_mutex_t *__mutex)

__THROW __nonnull ((1));

#ifdef __USE_XOPEN2K

/* Wait until lock becomes available, or speci?ed time passes. */

extern int pthread_mutex_timedlock (pthread_mutex_t *__restrict __mutex,

__const struct timespec *__restrict

__abstime) __THROW __nonnull ((1, 2)); #endif

/* Unlock a mutex. */

extern int pthread_mutex_unlock (pthread_mutex_t *__mutex)

__THROW __nonnull ((1));

loving Delly

yours, Neo

linux进程控制 实验报告

长安大学 操作系统实验报告 实验课程:操作系统 实验名称:linux进程控制 学院:信息学院 专业:软件工程 学号:2406090106 姓名:刘建 日期:2012-5-09

一、实验目的 熟悉进程的创建过程,了解系统调用函数fork() 和execl()。 二、实验内容 1、阅读实例代码fork1,并编辑、编译、运行,记录程序的运行结果,尝试给出合理的解释,查阅有关资料,掌握系统调用fork( )的用法,返回值的意义。 2、阅读实例代码fork2,并编辑、编译、运行,记录程序的运行结果,尝试给出合理的解释,查阅有关资料,掌握在程序中运行一个操作系统命令和运行一个程序的方法。 3、修改fork2,使之能把运行的命令和程序作为参数传给fork2。 三、设计思想 1、程序框架

pid = -1 pid = 0pid> 0 2、用到的文件系统调用函数 fork() 和execl() 四、调试过程 1、测试数据设计 (1)fork1 命名程序1: 编写程序1:

编译程序1: 运行程序1: (2)fork2

编写程序2: 运行程序2:

(3)修改fork2 编写修改程序2: 修改后的运行结果: 2、测试结果分析 (1)对于程序1:因为系统调用fork()函数是一次调用两次返回值,而且先生成子进程还是父进程是不确定的,所以第一次执行生成子进程的时候返回的pid = 0,判断pid!=-1,所以输出了I’m the child. I’m the parent. 第二次,执行父进程的时候,返回的是子进程的进程号pid> 0,即pid的值仍然不为-1,所以又输出了一次I’m the child. I’m the parent。 (2)对于程序2:第一次调用fork()函数时,由于执行的是子进程还是父进程是随机的,所以第一次对父进程返回的是子进程的进程号(大于0),即pid> 0,所以输出I’m the parent. Program end.当第二次执行子进程时返回值是0,即pid = 0,所以输出I’m the child. 并调用了execl()函数,查看了指定路径中的文件。

linux实验项目 进程控制操作

重庆电力高等专科学校 实 验 报 告 书 实验名称:Linux文件命令学院:信息工程学院 指导老师:黄泽伟 班级:软件1311 学号:201303100243 姓名:周贵波

实验项目进程控制操作 一、实验目的 1.了解进程的概念。 2.熟悉Linux的前台与后台进程控制操作。 3.掌握利用进程监控工具来维护系统的正常运行。 二、实验环境 安装了Red Hat Linux9.0和windows虚拟机系统的计算机一台。 三、实验内容 1.Linux的前台与后台进程控制操作。 2.系统监视器(system monitor)的使用和系统维护。 四、实验步骤 1.进程的前台工作方式 1)yes ok ←┘ 终端窗口显示___ok___,此时键盘能否输入其它命令____不能___,为什么? 2)按键:Ctrl+Z,暂停一个前台进程的运行,并转为挂起进程。 3)jobs ←┘记录下看到的结果_____yes ok_________________。 4)fg %1 ←┘作用:把挂起进程转为前台进程执行; 5)再按键:Ctrl+Z 作用与2)相同; 6)jobs ←┘记录下看到的结果________yes ok______________。 7)bg %1 ←┘作用:重新运行挂起进程,但以后台方式运行; 8)此时能否再按键:Ctrl+Z暂停一个后台进程的运行?____不能__,

键盘能否输入其它命令__不能_____,为什么? 9)用鼠标点击,关闭终端窗口。 2.进程的后台工作方式 除上面把前台进程转到后台进程的过程外,一般在shell提示符下,输入的命令后加上&,即以后台方式运行命令。再次进入终端方式。 1)xclock & ←┘后台进程1,记录其时钟的时间___16:40____ xcalc & ←┘后台进程2 find / -name ?*.jpg‘–print > templist & ←┘后台进程3 2)jobs ←┘有几个后台进程:__2______,记录显示结果: xclock xcalc & ps ←┘记录下看到的结果: 4036 pts/0 00:00:00 bash 4061 pts/0 00:00:00 xcalc 4063 pts/0 00:00:00 ps 3)fg %1 ←┘将时钟进程转到前台运行。 4)按键:Ctrl+Z, 将时钟进程挂起,记录时钟的时间___16:50______。 jobs ←┘观察显示结果____xcalc &________________。 回顾上述操作,经过二~三分钟后,看图形时钟有走动吗?____否___。 5)bg %1 ←┘ 将挂起的时钟进程转到后运行,现在再观察时钟是否会有变化,为什么? 有后台继续运行 jobs ←┘ kill %2 ←┘杀死计算器进程,看计算器是否消失__否______。 6)ps ←┘ 观察屏幕显示,记录时钟(xclock)进程的进程号pid=__________。 7)kill ←┘ pid为xclock的进程号,作用__newline____________。 试比较与上述步骤5)中的kill命令的区别。 8)ls –l ←┘是否有templist文件? ____否____。 9)rm templist ←┘删除临时文件。

Linux 进程管理实验

Linux 进程管理实验 一、实验内容: 1. 利用bochs观测linux0.11下的PCB进程控制结构。 2. 利用bochs观测linux0.11下的fork.c源代码文件,简单分析其中的重要函数。 3. 在fork.c适当位置添加代码,以验证fork函数的工作原理。 二、Linux进程管理机制分析 Linux有两类进程:一类是普通用户进程,一类是系统进程,它既可以在用户空间运行,又可以通过系统调用进入内核空间,并在内核空间运行;另一类叫做内核进程,这种进程只能在内核空间运行。在以i386为平台的Linux系统中,进程由进程控制块,系统堆栈,用户堆栈,程序代码及数据段组成。Linux系统中的每一个用户进程有两个堆栈:一个叫做用户堆栈,它是进程运行在用户空间时使用的堆栈;另一个叫做系统堆栈,它是用户进程运行在系统空间时使用的堆栈。 1.Linux进程的状态: Linux进程用进程控制块的state域记录了进程的当前状态,一个Linux 进程在它的生存期中,可以有下面6种状态。 1.就绪状态(TASK_RUNNING):在此状态下,进程已挂入就绪队列,进入准备运行状态。 2.运行状态(TASK_RUNNING):当进程正在运行时,它的state域中的值不改变。但是Linux会用一个专门指针(current)指向当前运行的

任务。 3.可中断等待状态(TASK_INTERRUPTIBLE):进程由于未获得它所申请的资源而处在等待状态。不管是资源有效或者中断唤醒信号都能使等待的进程脱离等待而进入就绪状态。即”浅睡眠状态”。 4.不可中断等待状态(TASK_UNINTERRUPTIBLE):这个等待状态与上面等待状态的区别在于只有当它申请的资源有效时才能被唤醒,而其它信号不能。即“深睡眠状态”。 5.停止状态(TASK_STOPPED):当进程收到一个SIGSTOP信号后就由运行状态进入停止状态,当收到一个SINCONT信号时,又会恢复运行状态。挂起状态。 6.终止状态(TASK_ZOMBIE):进程因某种原因终止运行,但进程控制块尚未注销。即“僵死状态”。 状态图如下所示: 2.Linux进程控制块:

Linux程序设计上机指导书3:Linux进程控制

上机三:Linux进程控制 1.目的 (1)掌握系统调用fork(),exex(),exit()等实现进程创建; (2)掌握进程的终止方式(return、exit、_exit、abort); (3)掌握僵尸进程的产生和避免,以及wait,waitpid的使用; (4)了解守护进程的创建。 2.内容 主要上机分析代码文件。 systemtest.c 6-3.c 6-4.c 6-8.c 6-9.c 其他略。 3.步骤 1)Linux进程的创建 创建进程可以采用几种方式。可以执行一个程序(这会导致新进程的创建),也可以在程序内调用一个fork 或exec来创建新进程。fork 调用会导致创建一个子进程,而exec 调用则会用新程序代替当前进程上下文。exec系列函数并不创建新进程,调用exec前后的进程ID是相同的。

exec函数的主要工作是清除父进程的可执行代码映像,用新程序的代码覆盖调用exec 的进程代码。如果exec执行成功,进程将从新程序的main函数入口开始执行。调用exec 后,除进程ID保持不变外,还有下列进程属性也保持不变。 (1)进程的父进程ID。 (2)实际用户ID和实际用户组ID。 (3)进程组ID、会话ID和控制终端。 (4)定时器的剩余时间。 (5)当前工作目录及根目录。 (6)文件创建掩码UMASK。 (7)进程的信号掩码。 与exec系统调用不同,system将外部可执行程序加载执行完毕后继续返回调用进程。 【例6.3】设计一个程序,用fork函数创建一个子进程,在子进程中,要求显示子进程号与父进程号,然后显示当前目录下的文件信息,在父进程中同样显示子进程号与父进程号。

c语言:流程控制语句

流程控制 ======= 知识点: if-else语句使用 switch语句使用 break使用 正常情况下,程序从main函数开始,一句一句,自上而下执行每一条语句。 一些语句可以控制,修改程序的运行流程,称为流程控制语句。 一、条件判断语句if if(表达式){ 语句 } 如果……就…… 【逻辑】当程序执行到if语句,首先计算表达式的值,如果为真,执行语句。如果为假,不执行、跳过语句。 【逻辑假设】 if(表达式){ 语句1 }else{ 语句2 } 如果……就……否则…… 【逻辑】当程序执行到if else语句,首先计算表达式的值,如果为真,执行语句1,不执行语句2。 如果为假,执行语句2,不执行语句1。 【逻辑二择】 例,明天考试,80分以下把代码抄写五遍,80分以上奖励棒棒糖一枚。 if(表达式1){ 语句1 }else if(表达式2){ 语句2 }else if(表达式3){ 语句3 } else{ 语句4 } 如果(表达式1成立),那么执行语句1 否则如果(表达式2成立),那么执行语句2 …… 否则,执行语句4。

【逻辑】当程序执行到该结构,计算机表达式1,如果成立,执行语句1,跳出整个结构。如果表达式2为假,计算表达式2,如果为真,执行语句2,跳出结构体。直到表达式2为假,计算表达式3,依次类推,如果表达式都不成立,执行else下的语句。 如果不需要(即所有表达式都不成立,什么都不做),可以不写else。else if的数量根据需求,任意增减,没有限制。 【注】在if else if结构中,每个else都是对前面每个表达式的否定。 【逻辑多择】 嵌套的if-else语句 我们可以在if语句内嵌套if语句,同样,我们也可以在if语句内嵌套if-else语句,在if-else 语句内嵌套if语句,以及在if-else语句内嵌套if-else语句。 int x; scanf(“%d”,&x); if(x<0){ printf(“error!\n”); }else{ if(x%2) printf(“jishu\n”); else printf(“oushu\n”); } 练习 1、使用if else if,模拟下述分段函数。 x^2+1(x<0) y= 2*x+6 (0<=x<=5) 3*x-1 (x>5) 2、输入一个字符,如果是大写字母,转成小写字母。如果是小写字母,转成大写字母,如果不是字母,不变输出。 3、输入一个字符,判断这个字符是表示大写字母,小写字母,数字字符,还是其他。A a 9 % 二、逻辑跳转语句goto switch 1、goto语句 无条件跳转到指定标号的位置,向下执行。 goto 语句标号 printf(“————————1\n”); printf(“————————2\n”); goto A; //跳转到标号的位置 printf(“————————3\n”); printf(“————————4\n”); //标号的名字是标识符,这里使用大写是为了和变量相回避。 A: //这是一个标号,是一个位置的标记,本身是什么都不做。 printf(“————————5\n”); B:

linux进程管理篇

目录:(内容较多,加个目录) |-进程管理 进程常用命令 |- w查看当前系统信息 |- ps进程查看命令 |- kill终止进程 |- 一个存放内存中的特殊目录/proc |- 进程的优先级 |- 进程的挂起与恢复 |- 通过top命令查看进程 计划任务 |- 计划任务的重要性 |- 一次性计划at和batch |- 周期性计划crontab 进程管理的概念 进程和程序区别 1.程序是静态概念,本身作为一种软件资源长期保存;而进程是程序的执行过程,它是动态概念,有一定的生命期,是动态产生和消亡的。 2.程序和进程无一一对应关系。一个程序可以由多个时程公用;另一一方面,一个进程在活动中有可顺序地执行若干个程序 父子进程的关系 1.子进程是由一个进程所产生的进程,产生这个子进程的进程称为父进程 2.在linux系统中,使用系统调用fork创建进程。fork复制的内容包括父进程的数据和堆栈段以及父进程的进程环境。 3.父进程终止子进程自然终止。 前台进程和后台进程 前台进程 在shell提示处理打入命令后,创建一个子进程,运行命令,Shell等待命令退出,然后返回到对用户给出提示符。这条命令与Shell异步运行,即在前台运行,用户在它完成之前不能执行别一个命令

很简单,我们在执行这个查找命令时,无法进行其它操作,这个查找就属于前台进程 后台进程 在Shell提示处打入命令,若后随一个&,Shell创建子进程运行此命令,但不等待命令退出,而直接返回到对用户给出提示。这条命令与Shell同步运行,即在后台运行。“后台进程必须是非交互式的” 再来看这个命令就变成了后台进程,我们用同样的条件进行查找,把查找记过放到hzh/test/init.find这个文件中。不影响我们前台其它的操作。 进程的状态

2011180021-Linux操作系统-课程设计报告-基于Linux的进程调度模拟程序

河南中医学院 《linux操作系统》课程设计报告 题目:基于Linux的进程调度模拟程序 所在院系:信息技术学院 专业年级:2011级计算机科学与技术完成学生:2011180021 郭姗 指导教师:阮晓龙 完成日期:201X 年06 月22 日 目录 1. 课程设计题目概述3 2. 研究内容与目的4 3. 研究方法5 4. 研究报告6 5. 测试报告/实验报告7 6. 课题研究结论8 7. 总结9

1、课程设计题目概述 随着Linux系统的逐渐推广,它被越来越多的计算机用户所了解和应用. Linux是一个多任务的操作系统,也就是说,在同一个时间内,可以有多个进程同时执行。如果读者对计算机硬件体系有一定了解的话,会知道我们大家常用的单CPU计算机实际上在一个时间片断内只能执行一条指令,那么Linux是如何实现多进程同时执行的呢?原来Linux使用了一种称为"进程调度(process scheduling)"的手段,首先,为每个进程指派一定的运行时间,这个时间通常很短,短到以毫秒为单位,然后依照某种规则,从众多进程中挑选一个投入运行,其他的进程暂时等待,当正在运行的那个进程时间耗尽,或执行完毕退出,或因某种原因暂停,Linux就会重新进行调度,挑选下一个进程投入运行。因为每个进程占用的时间片都很短,在我们使用者的角度来看,就好像多个进程同时运行一样了。本文就是对进程调度进行研究、实验的。 本文首先对Linux系统进行了简要的介绍, 然后介绍了进程管理的相关理论知识。其次,又介绍最高优先数优先的调度算法(即把处理机分配给优先数最高的进程)、先来先服务算法的相关知识,并对进程调度进行最高优先数优先的调度算法和先来先服务算法模拟实验,并对比分析两种算法的优缺点,从而加深对进程概念和进程调度过程/算法的理解 设计目的:在多道程序和多任务系统中,系统内同时处于就绪状态的进程可能有若干个。也就是说能运行的进程数大于处理机个数。为了使系统中的进程能有条不紊地工作,必须选用某种调度策略,选择某一进程占用处理机。使得系统中的进程能够有条不紊的运行,同时提高处理机的利用率以及系统的性能。所以设计模拟进程调度算法(最高优先数优先的调度算法、先来先服务算法),以巩固和加深处理进程的概念,并且分析这两种算法的优缺点。关键词:linux 进程调度调度算法

linux进程管理(一)

进程介绍 程序和进程 程序是为了完成某种任务而设计的软件,比如OpenOffice是程序。什么是进程呢?进程就是运行中的程序。 一个运行着的程序,可能有多个进程。比如自学it网所用的WWW服务器是apache服务器,当管理员启动服务后,可能会有好多人来访问,也就是说许多用户来同时请求httpd服务,apache服务器将会创建有多个httpd进程来对其进行服务。 进程分类 进程一般分为交互进程、批处理进程和守护进程三类。 值得一提的是守护进程总是活跃的,一般是后台运行,守护进程一般是由系统在开机时通过脚本自动激活启动或超级管理用户root来启动。比如在 Redhat中,我们可以定义httpd 服务器的启动脚本的运行级别,此文件位于/etc/init.d目录下,文件名是httpd,/etc/init.d/httpd 就是httpd服务器的守护程序,当把它的运行级别设置为3和5时,当系统启动时,它会跟着启动。 [root@localhost ~]# chkconfig --level 35 httpd on 进程的属性: 进程ID(PID):是唯一的数值,用来区分进程; 父进程和父进程的ID(PPID); 启动进程的用户ID(UID)和所归属的组(GID); 进程状态:状态分为运行R、休眠S、僵尸Z; 进程执行的优先级; 进程所连接的终端名; 进程资源占用:比如占用资源大小(内存、CPU占用量); 父进程和子进程: 他们的关系是管理和被管理的关系,当父进程终止时,子进程也随之而终止。但子进程终止,父进程并不一定终止。比如httpd服务器运行时,我们可以杀掉其子进程,父进程并不会因为子进程的终止而终止。 在进程管理中,当我们发现占用资源过多,或无法控制的进程时,应该杀死它,以保护系统的稳定安全运行; 进程管理

【IT专家】Linux中进程的几种状态

本文由我司收集整编,推荐下载,如有疑问,请与我司联系 Linux中进程的几种状态 2015/03/29 4411 1R (task_running) :可执行状态 ?只有在该状态的进程才可能在CPU上运行。而同一时刻可能有多个进程处于可执行状态,这些进程的task_struct结构(进程控制块)被放入对应CPU的可执行队列中(一个进程最多只能出现在一个CPU的可执行队列中)。进程调度器的任务就是从各个CPU的可执行队列中分别选择一个进程在该CPU上运行。 ?很多操作系统教科书将正在CPU上执行的进程定义为RUNNING状态、而将可执行但是尚未被调度执行的进程定义为READY状态,这两种状态在linux下统一为TASK_RUNNING状态。 ? 2S (task_interruptible):可中断的睡眠状态 ?处于这个状态的进程因为等待某某事件的发生(比如等待socket连接、等待信号量),而被挂起。这些进程的task_struct结构被放入对应事件的等待队列中。当这些事件发生时(由外部中断触发、或由其他进程触发),对应的等待队列中的一个或多个进程将被唤醒。 ?通过ps命令我们会看到,一般情况下,进程列表中的绝大多数进程都处于task_interruptible状态(除非机器的负载很高)。毕竟CPU就这么一两个,进程动辄几十上百个,如果不是绝大多数进程都在睡眠,CPU又怎么响应得过来。 ? 3D (task_uninterrupible)不可中断的睡眠状态 ?与task_interruptible状态类似,进程处于睡眠状态,但是此刻进程是不可中断的。不可中断,指的并不是CPU不响应外部硬件的中断,而是指进程不响应异步信号。绝大多数情况下,进程处在睡眠状态时,总是应该能够响应异步信号的。但是uninterruptible sleep状态的进程不接受外来的任何信号,因此无法用kill杀掉这些处于D状态的进程,无论是”kill”, “kill -9″还是”kill -15″,这种情况下,一个可选的方法就是reboot。

C语言中流程控制语句

C语言中流程控制语句(在Turbo C2.0中) (if, while,do-while,continue,break, for, switch,return) _1981/blog/item/528c122dcca9f335359bf73e.html 条件语句的一般形式为: if(表达式) 语句1; else 语句2; 上述结构表示: 如果表达式的值为非0(TURE)即真, 则执行语句1, 执行完语句1从语句2后开始继续向下执行; 如果表达式的值为0(FALSE)即假, 则跳过语句1而执行语句2。 注意: 1. 条件执行语句中"else 语句2;"部分是选择项, 可以缺省, 此时条件语句变成: if(表达式) 语句1; 表示若表达式的值为非0则执行语句1 , 否则跳过语句1继续执行。 2. 如果语句1或语句2有多于一条语句要执行时, 必须使用"{"和"}" 把这些语句包括在其中, 此时条件语句形式为: if(表达式) { 语句体1; } else { 语句体2; } 3. 条件语句可以嵌套, 这种情况经常碰到, 但条件嵌套语句容易出错, 其原因主要是不知道哪个if对应哪else。 例如: if(x>20||x<-10) if(y<=100&&y>x) printf("Good"); else printf("Bad"); 对于上述情况, Turbo C2.0规定: else语句与最近的一个if语句匹配, 上例中的else与if(y<=100&&y>x)相匹配。为了使else与if(x>20||x<-10)相匹配, 必须用花括号。如下所示: if(x>20||x<-10) { if(y<=100&&y>x) printf("Good"); } else printf("Bad"); 4. 可用阶梯式if-else-if结构。 阶梯式结构的一般形式为: if(表达式1)

Linux进程管理-实验报告

(1) 加深对进程概念的理解,明确进程和程序的区别。 (2) 进一步认识并发执行的实质。 (3) 学习通过进程执行新的U 标程序的方法。 (4) 了解Linux 系统中进程信号处理的基本原理。 Red Hat Linux (1)进程的创建 编写一段程序,使用系统调用fork ()创建两个子进程,当此进程运 行时,在系统中有一个父进程和两个子进程活动,让每一个进程在 屏幕上显示一个字符,父进程显示字符“a” ;子进程分别显示字符 “b”和字符“c” ,试观察记录屏幕上的显示结果,并分析原因。 程序代码: # include int main() { int pl ,p2 ; while((p 1 =fork())==-1); if(pl==0) putchar(b); else { while((p2=fork())==-1); if(p2==0) putchar( c 1); else putchar( a r ); } return 0; ) 运行结果:bca 分析:第一个while 里调用fork()函数一次,返回两次。子进程P1 得到的返回值是0,父进程得到的返回值是新子进程的进程ID (正 整数);接下来父进程和子进程P1两个分支运行,判断P1二二0,子 进程P1符合条件,输出%";接下来else 里面的while 里再调用 fork()函数一次,子进程P2得到的返回值是0,父进程得到的返回值 是新子进程的进程ID (正整数);接下来判断P2=:0,子进程P2符 合条件,输出,接下来父进程输出“a” ,程序结束。 (2)进程的控制 ① 修改已编写的程序,将每个进程输出一个字符改为每个进程输出 一 《Linux 实验目的: 实验环境: 实验内容: 操作系统设计实践》实验一:进程管理

Linux课程设计进程控制

课程设计报告 课程名称Linux操作系统课程设计 指导教师张玲 起止日期2014-03-01 至2014-06-13 实验项目实验二进程控制 学院信息与通信工程学院 专业电子信息工程 学生姓名 班级/学号 成绩 指导老师签字

1. 课程设计概述 本次课设意在利用进程控制相关的系统调用编程进行进程控制实验,观察进程从创建、运行到终止的控制过程,加深对进程概念的理解。 1.1 课程设计的目的 本实验的目的是掌握进程的概念,理解进程的创建、执行、等待、终止的过程。熟悉有关进程控制的命令和系统调用,理解Shell 的工作原理。 1.2 课程设计的内容 1) 进程控制命令 ● 使用进程控制命令ps ,kill 监视和控制进程的活动 2) 编程部分 ● 用fork ,wait ,exec 系统调用编程实现进程控制; ● 编写一个简单的shell 。 1.3 设计原理 进程是一个其中运行着一个或多个线程的地址空间和这些线程所需要的系统资源。 构建的文件构架如图: 图1 实验目录树的结构 2012010948 exp2 exp1 exp3 vis vis2

2.实验步骤 2.1操作 2.1.1进程控制命令(在此终端窗口的操作截图见图1) 1.执行ps命令,查看bash进程的PID: PID就是各进程的身份标识,程序一运行系统就会自动分配给进程一个独一无二的PID。进程中止后PID被系统回收。 2.在后台运行一个yes进程: yes > /dev/null & 3.用ps -f命令观察yes进程的pid和ppid,用ps u命令观察yes进程的状态。 yes进程的父进程是哪个进程?通过查看yes程序的PPID可知其父进程为bash 4.根据命令名搜索特定的进程: ps-ef|grep yes: 5.用kill命令杀掉yes进程: kill 【PID】: 图1 进程控制命令

实验一——Linux环境下的进程管理之欧阳光明创编

软件学院 欧阳光明(2021.03.07) 上机实验报告 课程名称:操作系统 实验项目:实验一:Linux环境下进程管理 实验室:耘慧402 姓名:学号: 专业班级:实验时间:

一、实验目的及要求 1.加深对进程概念的理解,明确进程和程序的区别; 2.进一步认识并发执行的实质; 3.分析进程争用资源的现象,学习解决进程互斥的方法; 4.了解Linux系统中进程通信的基本原理; 二、实验性质 1.进程的创建:编写一段程序,使用系统调用fork()创建两个子进程。当此程序运行时,在系统中有一个父进程和两个子进程活动。让每一个进程在屏幕上显示一个字符:父进程显示字符“a”;子进程分别显示字符“b”和字符“c”。试观察记录屏幕上的显示结果,并分析原因。 2.进程的控制:修改已编写的程序,将每个进程输出一个字符改为每个进程输出一句话,再观察程序执行时屏幕上出现的现象,并分析原因。如果在程序中使用系统调用lockf ()来给每一个进程加锁,可以实现进程之间的互斥,观察并分析出现的现象。 3.用fork( )创建一个进程,再调用exec( )用新的程序替换该子进程的内容;利用wait( )来控制进程执行顺序。 三、实验学时 实验性质:验证性 实验学时: 4学时 实验要求:必做 四、实验环境 1.实验环境: Linux系统开发环境 2.知识准备: (1) Linux系统开发环境搭建; (2) Linux环境下GCC编译器的使用; (3)语言中函数定义与调用、指针和类型的定义与使用、结构的定义、动态

内存的申请等预备知识。 五、实验内容及步骤 ①实验内容: (1)进程的创建 编写一段程序,使用系统调用fork()创建两个子进程。当此程序运行时,在系统中有一个父进程和两个子进程活动。让每一个进程在屏幕上显示一个字符:父进程显示字符“a”;子进程分别显示字符“b”和字符“c”。试观察记录屏幕上的显示结果,并分析原因。 (2)进程的控制 修改已编写的程序,将每个进程输出一个字符改为每个进程输出一句话,再观察程序执行时屏幕上出现的现象,并分析原因。 如果在程序中使用系统调用lockf()来给每一个进程加锁,可以实现进程之间的互斥,观察并分析出现的现象。 (3)用fork( )创建一个进程,再调用exec( )用新的程序替换该子进程的内容;利用wait( )来控制进程执行顺序。 ②实验步骤: 1.进程的创建 1.1 进程 UNIX中,进程既是一个独立拥有资源的基本单位,又是一个独立调度的基本单位。一个进程实体由若干个区(段)组成,包括程序区、数据区、栈区、共享存储区等。每个区又分为若干页,每个进程配置有唯一的进程控制块PCB,用于控制和管理进程。 PCB的数据结构如下: (1)进程表项(Process Table Entry)。包括一些最常用的核心数据: 进程标识符PID、用户标识符UID、进程状态、事件描述符、进程和U区在内存或外存的地址、软中断信号、计时域、进程的大小、偏置值nice、指向就绪队列中下一个PCB的指针P_Link、指向U区进程正文、数据及栈在内存区域的指针。 (2)U区(U Area)。用于存放进程表项的一些扩充信息。 每一个进程都有一个私用的U区,其中含有:进程表项指针、真正用户标识符

最新整理Linux操作系统的进程管理详解

L i n u x操作系统的进程管理详解 L i n u x操作系统中进程的管理是很重要的一部分,下面由学习啦小编为大家整理了L i n u x操作系统的进程管理详解的相关知识,希望对大家有帮助! L i n u x操作系统的进程管理详解 对于L i n u x的进程的管理,是通过进程管理工具实现的,比如p s、k i l l、p g r e p等工具; L i n u x操作系统的进程管理/1、 p s监视进程工具; p s为我们提供了进程的一次性的查看,它所提供的查看结果并不动态连续的;如果想对进程时间监控,应该用t o p工具; 1.p s的参数说明; p s提供了很多的选项参数,常用的有以下几个; l长格式输出; u按用户名和启动时间的顺序来显示进程; j用任务格式来显示进程; f用树形格式来显示进程; a显示所有用户的所有进程(包括其它用户); x显示无控制终端的进程; r显示运行中的进程;

w w避免详细参数被截断; 我们常用的选项是组合是a u x或l a x,还有参数f 的应用; p s a u x或l a x输出的解释; U S E R进程的属主; P I D进程的I D; P P I D父进程; %C P U进程占用的C P U百分比; %M E M占用内存的百分比; N I进程的N I C E值,数值大,表示较少占用C P U时间; V S Z进程虚拟大小; R S S驻留中页的数量; W C H A N T T Y终端I D S T A T进程状态 D U n i n t e r r u p t i b l e s l e e p(u s u a l l y I O) R正在运行可中在队列中可过行的; S处于休眠状态; T停止或被追踪;

C语言源程序

C语言程序设计 <一> 1、程序运行从main()函数开始,也从main()函数结束。 2、函数是C程序的基本单位,由函数头和函数体组成。 3、C语言源程序是由函数组成,它们的数目可以有多个。 4、在C语言源程序中,是必须有一个函数名为main的函数。main()后的小括号()没有任何内容,它不可以省略。 5、C程序中的语句用分号结束,在一个程序行上可以有多个语句,一个C 语句能写在多个程序行上。 6、函数体是由一对大括号围起来语句序列组成。小括号是函数的标志。 二、C语言的组成元素 1、命令、函数和变量等是组成程序的基本对象,每一个程序对象都需要一个名字来标识,程序对象的名称就是标识符。 2、在C语言中,标识符分为用户标识符和系统标识符。 3、用户标识符是指在程序中由程序员为变量、函数、数组等程序对象自行命名的标识符。 4、系统标识符是指C语言编译系统内部预定义的用于表示命令数据内型的标识符,又称保留字符。 5、基本字符集 英文字母:a~z和A~Z。 阿拉伯数字:0~9。其他字符:()、[]、+、-、*、/、%、<、>、<=、>=、==、!=、!、&&、‖、++、--、+=、-=、*=、/=、%=。它们是由1~2个字符组成。用作C语言的运算符;还包括:空格、换行符,回车符,单双引号,大括号,逗号,分号,反斜线,它们在程序中起到分隔和定界作用。 6、标识符:是用于标识命令;变量、函数,数组,数据类型等程序对象名称的字符序列。 7、组成标识符的合法字符有:字母,数字,下划线,三种,数字不能作首字符。 三、执行C语言程序 1、单击“开始”——“程序”——“附件”——“命令提示符”。 2、按功能键<10>激活菜单,执行“File”——“NEW”(ALT+回车)全屏。 3、为保存,输入程序文件名。 4、+运行 有输入:等待输入→回车 通过 无输入:返回到编辑 未通过:回车→F6→修改 F3(打开)

实验五 Linux进程管理

实验五Linux进程管理一.实验目的: 1.进行系统进程管理。 2.周期性任务安排 二.实验内容: 1.进程状态查看; 2.控制系统中运行的进程; 3.安排一次性和周期性自动执行的后台进程; 三.实验练习: 任务一进程管理 实验内容: 1、查看系统中的进程; 2、控制系统中的进程; 3、安排计划任务。 实验步骤: 1.使用ps命令查看和控制进程 显示本用户的进程: 显示所有用户的进程:。 在后台运行cat命令:

查看进程cat : 杀死进程cat:。 再次查看进程cat,看看是否被杀死。 注:ps命令包括较丰富的可选参数,常见的可选参数包括如下几个。 -A:显示所有用户启动的进程。 -a:显示所有其他用户的进程。 -u::显示进程拥有者、进程启动时间等更详细的信息。 -x:显示不是终端提供的进程信息。 -r:只显示正在运行的进程。 -m:显示线程信息。 -w:宽行显示进程信息。 -l:用长格式显示进程信息。 -t::只显示由终端/dev/tty提交的信息。 2.使用top命令查看和控制进程 用top命令动态显示当前的进程。 只显示用户user01的进程(利用u键)。 利用k键,杀死指定进程号的进程。 top参数: -b 以批量模式运行,但不能接受命令行输入;

-c 显示命令行,而不仅仅是命令名; -d N 显示两次刷新时间的间隔,比如-d 5,表示两次刷新间隔为5秒; -i 禁止显示空闲进程或僵尸进程; -n NUM 显示更新次数,然后退出。比如-n 5,表示top更新5次数据就退出;-p PID 仅监视指定进程的ID;PID是一个数值; -q 不经任何延时就刷新; -s 安全模式运行,禁用一些效互指令; -S 累积模式,输出每个进程的总的CPU时间,包括已死的子进程; 交互式命令键位: space 立即更新; c 切换到命令名显示,或显示整个命令(包括参数); f,F 增加显示字段,或删除显示字段; h, 显示有关安全模式及累积模式的帮助信息; k 提示输入要杀死的进程ID,目的是用来杀死该进程(默人信号为15) i 禁止空闲进程和僵尸进程; l 切换到显法负载平均值和正常运行的时间等信息; m 切换到内存信息,并以内存占用大小排序; n 提示显示的进程数,比如输入3,就在整屏上显示3个进程;

linux中用信号进行进程时延控制

Linux下使用信号进行进程的运行控制 1linux的信号 信号全称为软中断信号,也有人称作软中断,是Linux系统中的最古老的进程间通讯方式。它们用来向一个或多个进程发送异步事件信号。信号可以从键盘中断中产生,另外进程对虚拟内存的非法存取等系统错误环境下也会有信号产生。信号还被shell程序用来向其子进程发送任务控制命令。 2系统调用介绍 2.1 alarm系统调用 #include unsigned int alarm(unsigned int seconds); alarm()用来设置信号SIGALRM在经过参数seconds指定的秒数后传送给目前的进程。如果参数seconds为0,则之前设置的闹钟会被取消,并将剩下的时间返回。返回之前闹钟的剩余秒数,如果之前未设闹钟则返回0。 2.2Signal #include typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler); signal系统调用用来说明对信号的处理方法。Signum参数用来指定信号,handler可以 如果是信号处理函数,则signal会在收到信号后调用该函数,我们可以形象的称为信号注册处理函数。 2.3kill系统调用 #include #include int kill(pid_t pid,int sig); kill函数用于给进程组或进程发送信号。pid为目标进程号,sig为要被发送的信号。发送信号的进程必须和目标进程在同一个进程组,或者发送i信号的进程的拥有者是一个超级用户,另外,进程可以向自己发送信号。 2.4pause系统调用 #include int pause(void); pause函数使调用进程挂起直到有信号递达。如果信号的处理动作是终止进程,则进程终止,pause函数没有机会返回;如果信号的处理动作是忽略,则进程继续处于挂起状态,pause不返回;如果信号的处理动作是捕捉,则调用了信号处理函数之后pause返回-1,errno设置为EINTR,所以pause只有出错的返回值。错误码EINTR表示“被信号中断”。 3进程自身时延控制 进程可以利用信号机制来实现对自身运行状态的控制。以下示例程序就是利用alarm()系统调用来时间程序对自身运行时间的控制。 #include #include #include

《C语言程序设计》基本知识点

《C语言程序设计》教学基本知识点 第一章C语言基本知识 1.C源程序的框架 尽管各个C源程序的功能千变万化,但框架是不变的,主要有:编译预处理、主函数()、函数n()等,主函数的位置不一定在最前面,可以在程序的中部或后面,主函数的名字固定为main。 2.C语言源程序的书写规则: (1)C源程序是由一个主函数和若干个其它函数组成的。 (2)函数名后必须有小括号,函数体放在大括号内。 (3)C程序必须用小写字母书写。 (4)每句的末尾加分号。 (5)可以一行多句。 (6)可以一句多行。 (7)可以在程序的任何位置加注释。 3.语句种类 语句是程序的基本成分,程序的执行就是通过一条条语句的执行而得以实现的,根据表现形式及功能的不同,C语言的基本语句可以分为五大类。 (1)流程控制语句 流程控制语句的功能是控制程序的走向,程序的流程有三种基本结构:顺序结构、分支结构和循环结构,任何复杂的程序都可以由这三种基本结构复合而成。其中后两种结构要用特定的流程控制语句实现。 (2)表达式语句 表达式语句的形式是:表达式;,即表达式后跟一分号“;”,分号是语句结束符,是一个语句必不可少的成分。表达式和表达式语句的区别在于表达式代表的是一个数值,而表达式语句则代表一种动作。最常见的表达式语句是赋值语句。 (3)函数调用语句 函数调用语句实际上也是一种表达式语句,形式为:在一次函数调用的小括号后面加上一个分号。 (4)空语句 空语句的形式就是一个分号,它不代表任何动作,常常作为一个意义转折点使用。 (5)复合语句 复合语句从形式上看是多个语句的组合,但在语法意义上它只相当于一个语句,在任何单一语句存在的地方都可以是复合语句。注意复合语句中最后一个语句末尾的分号不能少。复合语句右大括号后面没有分号。 4.运算符 用来表示数据各种操作的符号称为运算符。运算符实际上代表了一种类型数据的运算规则。不同的运算符具有不同的运算规则,其操作的数据类型必须符合该运算符的要求,运算结果的数据类型也是固定的。 根据参加操作的数据个数多少,可以将C语言的运算符分为单目运算符,双目运算符和三目运算符(三目运算符只有条件运算符一个)。 根据运算对象和运算结果的数据类型可分为算术运算符、关系运算符、逻辑运算符等。 5.表达式 表达式是由常量、变量、函数,通过运算符连接起来而形成的一个算式。一个常量,一个变量或一个函数都可以看成是一个表达式。 表达式的种类有: 算术表达式、关系表达式、逻辑表达式、赋值表达式、字位表达式、强制类型转换表达式、逗号

C语言程序的流程控制

第3章C语言程序的流程控制 利用计算机进行程序设计时,不仅仅只处理一些简单的数据,在大部分的程序设计里处理的问题还是相当复杂的。对于那些复杂的程序设计,读者不可能直接就能写出程序的源代码,而是要通过一些具体的设计方法(如用程序流程图)把程序设计思想先写出来,然后根据程序流程图编写代码。本章将具体介绍进行程序设计时使用的基本方法和C程序的流程控制。 3.1程序设计的基本知识 3.1.1程序流程图 在描述一个程序的基本结构思想时有很多种方法,其中程序流程图是最常用也是最基本的方法。 1.传统程序流程图 传统流程图表示法的特点是用一些图框表示各种类型的操作,用线表示这些操作的执行顺序。美国国家标准化协会ANSI规定了一些常用的流程图符号,现已为世界各国普遍采用。我国也有自己的国家标准GB 1526-89与该标准基本相同,本书就参照

ANSI标准做具体介绍。标准中各种图示如图所示。 起止框输入输出框判断框处理框 或 流程线连接点注释框 图传统流程图表示法 下面对其中一些主要符号作简要说明: (1)起止框是用来标识程序的开始和结束位置的。规定流程图以起止框开始,以起止框结束。 (2)输入输出框也叫数据框,其中可以注明数据名称、来源、用途或其它的文字说明。 (3)菱形框的作用是对一个给定的条件进行判断,根据给定的条件是否成立来决定如何执行其后的操作。 (4)处理框用矩形表示各种处理功能。例如,执行一个或一组特定的操作,从而使信息的值、信息的形式或所在位置发生变化。另外在矩形框内可注明特定处理名称或其简要功能。 (5)流程线用带箭头的直线表示程序

的执行顺序。当流程自左向右或自上向下时流程线可以不带箭头,其它情况应加箭头表示流程。 (6)连接点用小圆圈表示将画在不同地方的流程线连接起来。下图中有两个以①为标志的连接点,它表示这两个点是互联在一起的,实际它们是同一个点。这种连接通常用在图形画不下而需要分开画时。 ① ① (7)注释框不是流程图的必要部分,其不反映流程操作,只是为了流程图中某些操作做解释补充的,以帮助阅读流程图的人更好的理解流程图的作用。 2.N-S流程图 灵活的流程线在程序设计中有它自己的优点,但也隐藏着许多导致错误的祸根。因为它允许用流程线使流程任意转移,这对程序设计是一个隐患,它使程序流程看起来很乱,使程序难以理解和维护。针对这一弊病,1973年美国学者I . Nassi和B . Shneiderman 提出了一种无流程线的流程图,称为N-S图。

相关文档
最新文档