当前位置:文档之家› Developing Reusable Multi-Tasking Components Using Object-Oriented Techniques in Ada 95

Developing Reusable Multi-Tasking Components Using Object-Oriented Techniques in Ada 95

Developing Reusable Multi-Tasking Components Using Object-Oriented Techniques in Ada 95
Developing Reusable Multi-Tasking Components Using Object-Oriented Techniques in Ada 95

Developing Reusable Multi-Tasking Components Using Object-Oriented Techniques in Ada 95

Patrick de Bondeli

14 Boulevard Jean Mermoz

92200 Neuilly-s-Seine, France

EMail : bondeli@https://www.doczj.com/doc/ea2180374.html,

1. Introduction

Since 1985, we have undertaken extensive works on concurrent system development methodology and their systematic implementation in Ada. In 1990-1991, this resulted in the presentation of the CODE-TOP (COncurrent system DEvelopment using Transformations Of Predicate-transition nets) methodology [1][2]. Starting from this basis, we later developed a framework of components, using OO techniques, which are reusable to implement in Ada 95 any type exporting synchronization / communication properties [3].

This leads us to propose, as a theme for this workshop, the identification, development, and implementation in Ada 95 of general purpose reusable components for concurrent systems. By "general purpose", we mean that these components are not specialized in any specific domain as, for example, communication, or guidance and control systems. This proposal enters perfectly within the goals that were stated in the "Call for Participation" and is more specifically related to the two following goals : ."Identify the benefits and impacts of using object-oriented programming in multi-tasking real-time systems";

."Explore the use of Ada 95 in developing multi-tasking components that are resilient to software design errors and hardware failures".

In section 2, we briefly expose the state of our works, which we already quoted.

In section 3, we propose to consider the different possible kinds of reusable multi-tasking components in Ada 95. On one kind of components our works could be a good starting point for discussion.

2. The Present State of Our Works in the Area of Reusable Components for Concurrent Systems

If we consider the general model of asynchronous p r o c e s s e s c o m m u n i c a t i n g t h r o u g h synchronization/communication objects, this model implies a partitioning of systems in three categories of objects : Active objects (processes), having their own thread of control, synchronization/communication objects, and purely passive objects which are to be used by a single process.

Our mentioned work was concerned in developing reusable components for the two first categories of objects.

Components for building active objects were considered only in the CODE-TOP study [1][2] and implemented in Ada 83. These components included a number of (formally defined) models of process activation, either through explicit operations, or by asynchronous interrupts (in this latter case, dynamic attachment/detachment operations were also included) and their implementation in Ada 83. The implementation in Ada 95 should be made a lot simpler and more flexible by the new possibilities of referencing task objects and the new interrupt model.

The CODE-TOP study also lead to the development of a methodology for (formally) specifying and implementing in Ada 83 any type of synchronization/communication objects. The framework of components presented in [3] is reusable to implement in Ada 95 any type of synchronization / communication from its CODE-TOP style of formal specification. An annex to the present paper gives further detail on it. We attempted to maximize the reusability of this framework by making a full use of object-oriented features of Ada 95 (tagged types, class-wide programming, hierarchical library units) and we found an interesting way (having more general applicability than those proposed in [4]) for implementing the mix of extensibility and concurrency properties which it exports.

3. The Different Kinds of Reusable Multi-Tasking Components which We Could Consider

3.1. Considering the Sole "Asynchronous Communicating Process" Model

A first item of discussion could be reusable components to build synchronization/communication objects.

The discussion on this item could be introduced by a presentation of the framework of Ada 95 components that we have developed for this purpose and which is briefly described in the annex to this paper. Such matters as domain (synchronization/communication) analysis, architectural and implementation choices could be considered in this discussion.

A second item of discussion could be active objects and the process activation operations : Are Ada 95 (core language plus system and real-time annexes) primitives to be directly used, or is there some room for a higher level set of reusable components in this area?

3.2. Beyond the "Asynchronous Communicating Process" Model

The asynchronous process model, by itself, does not allow a proper treatment of time constraints. It allows time-limited waiting operations and periodic activation of processes, but it provides no direct support to the specification and verification of time constraints.

For systems where processes are essentially activated periodically the Rate Monotonic Analysis (RMA) [5] allows the verification of time constraints while necessitating no deviation from the asynchronous process model. The Ada 95 Real-Time Annex offers an implementation support to RMA which did not exist in Ada 83. The proposal of reusable components supporting RMA in Ada 95 is probably a worthwhile discussion topic for us.

The synchronous model has been developed essentially in France (see [6] for example) to provide a tool for specifying time constraints in real-time systems. Practical implementations of systems using this type of specification have so far, to my knowledge, been limited to a sequentialization into a single automaton. Ada 95 tasking, with the presence of the ATC, may perhaps offer some hope for more efficient implementations, preserving the inherent concurrency in the model, but I have seen no reference yet of any work in this direction. Will a discussion on such a topic tempt some of the participants to our workshop?

4. Conclusion

In section 3 we have considered as possible discussion topics for our workshop the different areas for which general purpose (i.e. not application domain specific) multi-tasking reusable components could be proposed. But, to tell the truth, we have effectively performed works which can be used as a starting point for discussion only in the first proposed area (reusable components for building synchronization / communication objects). The state of these works is briefly presented in the annex to this paper.

References

[1] P. de Bondeli. Tutorial on ?Ada Oriented Methods for Real

Time Systems?. 9th DASC, Virginia Beach (VA, USA), 1990, IEEE/AIAA/NASA.

[2] P. de Bondeli. ?The CODE-TOP (...) Method. Examples of

Using it for System Architecture Specification, Software Specification and Code Generation?. Processing of the Internationap Symposium on Real-Time Embedded Processing for Space Applications, Les Saintes-Maries-de-la-Mer, 1992, Cepadues, Toulouse.

[3] P. de Bondeli. "A Fully Reusable Class of Objects for

Synchronization and Communication in Ada 9X". Proc. of the Symposium "Ada in Europe" , Copenhagen, September 1994, LNCS 887, Springer Verlag.

[4] A. Burns and A. Wellings. Concurrency in Ada.

Cambridge University Press, 1995.

[5] M. Klein, T. Ralya, B. Pollak and R. Obenza. A Practitioner's

Handbook for Real Time Analysis : Guide to Rate Monotonic Analysis for Real-Time Systems.

Kluwer Academic Publishers, 1993.

[6] G. Berry and G. Gonthier The ESTEREL Synchronous

Programming Lnaguage : Design, Semantics, Implementation. INRIA Report 842, 1988.[7] Draft Technical Report for Extensions for Real Time Ada.

ISO/IEC JTC1/SC22/WG9/RRG,

Work Item N° JTC 1.22.35, Vers. 5.0, 1994.

[8] Petri Nets : Central Model and their Properties.

LNCS 254, Springer Verlag, 1987.

Annex

A Fully Reusable Class of Objects for Synchronization and

Communication in Ada 95

1. Introduction

This document presents a very general class which can be reused to specify and implement any type exporting synchronization or communication properties.

Our main point here is to show how the new Ada 95 features modelling inheritance, polymorphism and hierarchies of library units can be used to describe the architecture of the class and how other new Ada 95 features (access to subprograms, protected types, ...) can be used for the specification and implementation of its components.

The class is composed of a number of subclasses; each subclass is defined by an Ada 95 package and exports a style of synchronization/communication ranging from "pure synchronization" to "timed selective communication"; the different subclasses are derived one from another by extension. The packages defining the subclasses, plus a number of packages containing common features, form a framework of reusable components; a user builds a specific synchronization/communication type by undertaking a form of reuse of the corresponding subclass known as specialization.

R e f e r r i n g t o c l a s s e s o f synchronization/communication objects which are presently offered as standards in the Ada world, namely protected types and the types exported by the "Real Time A d a E x t e n s i o n s"e l a b o r a t e d b y ISO/JTC1/SC22/WG9/RRG [7], our class is much more general. It exports kinds of operations which are neither offered by, nor simply built from, these standards, such as operations with waiting preconditions depending from some input parameters or selective communication operations offering communication on an object selected from several possible objects.

The class specification is made from Ada 95 package specifications supplemented by formal notations describing the dynamic semantics; a part of these formal notations is nested in the Ada package specifications as (formal) comments, and another part of them consists of predicate-transition nets, a high level form of Petri nets [8], describing the concurrency properties.

The work which is presented here is related to the CODE-TOP (COncurrent system DEvelopment using Transformations Of Predicate-transition nets) method which has been the subject of several papers [1] [2]; the present document however presents a new view of the same basic problem, in which extension replaces gathering of individual operation schemes and specialization replaces generic instantiation.

The remaining of this document is organized as follows :

Section 2 presents the general architecture of our class and of its reusable components.

Section 3 gives samples of specification and implementation of components of our class.

Finally, section 4 concludes in giving an overview of this framework and in stating how its rather specific type extension principle can be more generally used.

2. General Architecture

2.1. The Level of Operations to Be Exported by the Components of our Class

In order for our components to be widely reusable in a large domain (all synchronization/communication problems), the level of the operations which are exported by these components is unavoidably low. No specific queuing policy, no notion of point to point communication versus broadcasting may be incorporated since any of these must be implementable through the use of the operations we offer.

A consequence of this is that the natural users of our reusable components are specialists of synchronization/communication policies rather than the final application domains engineers; this is of course another difference with [7].

With that in mind, as in the case of the already quoted CODE-TOP method, we chose to export "elementary operation schemes". We call "elementary operation" an operation which makes an indivisible (by other operations on the same objects) use of one or several synchronization/communication object(s). An "elementary operation scheme" is an elementary operation which is parameterized by access to subprogram parameters (here, instead of generic subprogram parameters in the CODE-TOP case)

representing the waiting precondition and the actions which are attached to the operation.

We can limit to about 10 the number of different useful elementary operation schemes (theoretically a few more are conceivable, but their usefulness is very questionable). The basic elementary operation schemes are Change_Action (changing the state of an object with no possible effect on tasks blocked on it), Blocking_Action (an action which may block the calling task, depending on the state of the object to which it applies), Release_Action (changing the state of an object with possibly releasing tasks which were blocked on it as a consequence of their having called a blocking action). The other elementary operation schemes are indivisible combinations of blocking and changing or releasing, selective blocking actions, and "timed" actions (all of the above blocking actions with a blocking time being limited to a given duration).

The three basic elementary operation schemes (Change_Action, Blocking_Action, Release_Action) are mainly characterized by a different profile of their "Handler" parameter accessing to the actual action to be performed. The parameter profile of Change_Action_Handler includes the object attributes and input and output data. The parameter profile of Release_Action_Handler adds to the profile of Change_Action_Handler an extra parameter denoting the set of waiting tasks to be released. The parameter profile of Blocking_Action_Handler includes the object attributes, user characteristics on which the waiting precondition (determining the blocking / non-blocking of the caller) may depend and the result of the waiting precondition computation. Note that the elementary operations which are built on the Blocking_Action and Release_Action schemes, or on a non-basic scheme including a blocking and / or releasing action, perform all the queue management (the queues themselves being attributes of the synchronization / communication objects). Elementary operations which are built on the Change_Action scheme do not participate to queue management; thus they should not assign the queue attributes (unless their purpose is to model phenomenons like interferences or transmission losses ...).

2.2. General Principles for Implementing a Synchronization/Communication Type Specification Using our Reusable Components

In order to implement the specification of a synchronization/communication type T, exporting elementary and non-elementary operations, using our components, one proceeds in several successive steps :

(a). From the specification of T, point out all the necessary elementary operations which are necessary to implement the operations of T (that is the elementary operations which are directly exported by T, plus the elementary operations the combination of which makes each non-elementary operation exported by T).

(b). Chose the "smallest" reusable component of our class which exports all the elementary operation schemes mapping, by specialization, all the elementary operations which were pointed out in step (a).

(c). Build type T_Elem exporting all these elementary operations as specializations of operation schemes of the selected reusable component.

(d). Build all non-elementary operations exported by T as combinations of elementary operations exported by T_Elem.

2.3. A General View of the Architecture of our Reusable Components

In addition to synchronization/communication operation schemes, we must also export operation schemes mapping the creation and deletion of a synchronization/communication object. In practice two cases arise : Either we want "static" objects, which cannot be deleted, or we want "dynamic" objects, which can be deleted. The latter case is far more complicated than the former, because we also want the deletion of a synchronization/communication object causing the tasks which were blocked on this object to be released in order to prevent their deadlocking in such a case. It is then desirable to have a clear distinction between static and dynamic object subclasses in order to avoid inheriting the additional complexity of the latter case when we don't need it.

2.3.1. The Components Defining Subclasses of Static Objects

Figure 1 gives a general view of the architecture of these subclasses.

This is a very typical class architecture in Ada 95, consisting in a library package hierarchy having the package defining the items common to the whole class as its root and a child tree of packages where each package defines a type "Synchronization" (renamed "T" for brevity in the present paragraph) as an extension of its parent's type T, resulting in having a perfect mapping of library package units and inheritance within T'Class hierarchies. The reason of such an architecture is that types T attributes must be private in order of preventing direct assignments by their users, but these private attributes must also be made visible to the packages extending type T (since they must also, in general, define additional primitive subprograms accessing the private attributes), hence the compulsory use of the "is a child of" relation between packages, rather than the "with" relation, in conjunction with the "is a" relation between subclasses (each package, except Synch_Com, defining a new subclass).

The Synchronization_Communication Class (Static Types Components- Global View) Figure 1.

Detailing figure 1, one can see package Synch_Com, encapsulating the items which are common to the whole class and a tree of packages which are (transitive) children of Synch_Com and define each one of the subclasses. As we will see later on a more detailed view of some of these components, package Synch_Com.Synchronizations encapsulates a tagged private type Synchronization, which is the type of objects of the specific pure synchronization static objects subclass. The whole class of synchronization / communication objects is covered by type Synch_Com.Synchronizations.Synchronization'Class. Each child and transitive child of package Synch_Com.Synchronizations encapsulates a type Synchronization defined as a private extension of type Synchronization in its parent, which inherits the primitive elementary operations of the parent's type Synchronization, and defines new primitive elementary operations.

The "withed" package Waking_Conditions and its children export a type, Waiter, which is an elementary waiting position (or queue element) and a class, Waking_Condition'Class, which covers all information that is to be transmitted to a woken up Waiter object. These packages export the basic blocks which are used to define waiting structures (queues, waiting lists, waiting sets, ...) by Synch_Com (transitive) children.

Other lower level components (Sets, Queues, Tagged_Lists, Ordered_Lists, Timed_Events, ...), not appearing on figure 1, are used in the implementation of the above mentioned components.

2.3.2. The Components Defining Subclasses of Dynamic Objects

There is a tree of reusable components which is similar to the case of components for static objects. The r o o t o f t h e t r e e i s p a c k a g e Synch_Com.Synchronizations.Deletable.

Each subclass implements a protection allowing a single "object manager" (caller of a create or destroy operation) exclusively from the "object users" (callers of the other operations). Consequently, all the primitive operations must use this protection and package Synch_Com.Synchronizations.Deletable must override all the primitive operations inherited by type Synchronization by operations using this protection.

3. Samples of Detailed Specifications and Implementation of our Reusable Components

In [3] we give a complete specification of the Synch_Com, Synch_Com.Synchronizations and Waking_Conditions components and we discuss these specifications. A main point in this discussion is the presentation of types mixing extendibility and concurrency properties; two such types are exported by components Synch_Com.Synchronizations and Waking_Conditions respectively with interesting dual properties.

After that, [3] gives the specification of package Synch_https://www.doczj.com/doc/ea2180374.html,munications to demonstrate the extension mechanism within our class (the predicate-transition net part of the specification of Synch_https://www.doczj.com/doc/ea2180374.html,munications is omitted since it does not bring any further argument to this point).

In the present document we only give, as figures 2 and 3 respectively, a partial view of the specification and private part of packages Synch_Com and Synch_Com.Synchronizations in order to provide the reader with a flavour of our components.

[3] presents an implementation in Ada 95 of components Synch_Com, Synch_Com.Synchronizations and Waking_Conditions.

This section has two points of interest : It provides an example of the use of some program transformation techniques in the concurrent domain and it shows a few further interesting Ada 95 programming tricks involving private library unit children, protected types, class wide types and access to subprogram types.

Let us give a few more detail on points of this implementation which may be relevant for further discussion in the context of our workshop.

Type Waking_Conditions.Waiter, modelling the elementary waiting position, is a protected type which is the only one, in this framework, to have an entry (the queue of which may contain only one element, the task owning the waiter object). Entry queues cannot be used, in our framework, to implement waiting structures beyond this elementary waiting element for two reasons : they fail to provide the level of reusability which is the aim of the framework (they export one single waiting structure, the queue, with generally one or two possible queue handling policies which are likely not to be the same for all implementations) and they impede extendibility (there is no access_to_entry types) which we need within the framework (however the users do not normally perform further extensions).

Figures 2 and 3 give the private part of package Synch_Com (partially) and Synch_Com.Synchronizations respectively. They give enough information to let us explain the principle of the implementation and e x t e n s i b i l i t y o f t y p e Synch_Com.Synchronizations.Synchronization. On figure 3, we see that this type has the following attributes : Discriminant and Synchro_Data, which are respectively the constant and variable parts of the data attributes; Change_Action, Blocking_Action and Release_Action which are accesses to protected procedures (their types are given on figure 2). In order to insure their indivisibility, the three elementary operation schemes exported on the type (Change_Action, Blocking_Action, Release_Action) are implemented by protected procedures of an object of a protected type which is hidden in the package body (not given here). Function Create allocates this protected object and assigns the respective accesses to its protected procedures to attributes Change_Action, Blocking_Action, Release_Action, and these accesses are used by the body of the exported elementary operation schemes. When type Synchronization is extended in descendent packages, an access attribute is added for each new exported elementary operation scheme. Of course, inside the body the protected type is fully replaced by an "augmented" one; so, extension is performed only at the package specification level; at the body level, we have "replacement" rather than extension. But this solution is satisfactory since figure 1 shows that the number of these extensions/replacements is bounded to four (which limits code duplications of the protected operation bodies to a small bounded number), whereas the user of the framework sees an actual class hierarchy rooted at type Synch_Com.Synchronizations.Synchronization which he uses by specialization rather than by extension.

4. Conclusion

We have built a framework of components having the structure of a class covering all the types exporting synchronization/communication properties. This class is composed of two subtrees (one for static objects, one for dynamic objects) of five subclasses each. The five subclasses in each subtree are derived one from the other by extension and we have found, and briefly described in section 5, an interesting way of implementing in Ada 95 such extensions in a class exporting concurrency properties. We believe that this way of implementing extensibility within a concurrent type can be used in every case in which a small bounded number of extensions of the operations exporting concurrency properties is required. One may implement any type exporting synchronization/communication properties by specializing this framework in accordance with a process the principles of which are given in section 2.2. The detail of this specialization process uses formal specification techniques, based on predicate-transition nets, which are not described in this paper.

with Waking_Conditions; use Waking_Conditions;

package Synch_Com is

--provides a common root to the whole class of synchronization-communication objects;

type User_Characteristics is tagged null record;

function Default_U_Ch return User_Characteristics;

--User_Characteristics is the root type covering the specific user characteristics which waiting preconditions

--may depend on;

type Discriminant_Type is tagged null record;

function Default_Discriminant return Discriminant_Type;

type Discriminant_Access is access Discriminant_Type'Class;

--Discriminant_Type is the root type covering the discriminant part (constant attributes) of

--synchronization-communication objects;

type Synchronization_Data is abstract tagged null record;

function Default_S_D return Synchronization_Data;

type Synchronization_Data_Access is access Synchronization_Data'Class;

--Synchronization_Data is the root type covering the variable attributes of synchronization-communication objects; --The following three access procedure types define the 3 possible classes of actions which can be performed on

--the attributes of a synchronization-communication object.

type Change_Action_Handler is access procedure ( ...);

type Blocking_Action_Handler is access procedure ( ...);

type Release_Action_Handler is access procedure ( ...);

--

Null_Synchronization : exception;

--Null_Synchronization is raised in case of inconsistent use of a variable of one of the types Synchronization

--which are defined in the children packages;

private

...

--The following access protected procedure types are used to map the implementation of

--synchronization/communication operations onto attributes of the

--synchronization/communication subclasses;

type Access_Change_Action is access protected procedure ( ...);

type Access_Blocking_Action is access protected procedure ( ...);

type Access_Release_Action is access protected procedure ( ...);

{Declaration of access protected procedure types of no use in

Synch_Com.Synchronizations}

end Synch_Com;

Figure 2. Specification of Synch_Com

with Waking_Conditions; use Waking_Conditions;

package Synch_Com.Synchronizations is

--provides a common root for classes of non-deletable synchronization objects exporting a single class

--(the simplest one : Blocking_Action) of blocking operations;

type Synchronization is tagged private;

function Default_Synchronization return Synchronization;--initial state;

--

function Create

(Discr : Discriminant_Type'Class := Default_Discriminant;

D : Synchronization_Data'Class := Default_S_D) return Synchronization;

function Is_Discriminant (Of_Synchronization : Synchronization) return Discriminant_Type'Class;

procedure Change_Action (Handler : Change_Action_Handler;

Of_Synchronization : Synchronization;

D : Synchronization_Data'Class := Default_S_D;

D_Out : out Synchronization_Data'Class);

procedure Blocking_Action (Handler : Blocking_Action_Handler;

On_Synchronization : Synchronization;

My_Characteristics : User_Characteristics'Class := Default_U_Ch);

procedure Release_Action (Handler : Release_Action_Handler;

From_Synchronization : Synchronization;

D : Synchronization_Data'Class := Default_S_D;

D_Out : out Synchronization_Data'Class);

private

type Synchronization is tagged record

Discriminant : Discriminant_Access;

--Discriminant and Discriminant.all can be modified by function Create only;

Synchro_Data : Synchronization_Data_Access;

end record;

end Synch_Com.Synchronizations;

Figure 3. Specification of Package Synch_Com.Synchronizations

江苏大学大一c语言期末复习题汇总

选择题1.下列字符序列中,不可用作C语言标识符的是()。 A.abc123 B.C._123_ D._ok 2.请选出可用作C语言用户标识符的一组标识符()。 A.void B.a3_b3 C.For D.2a define _123 -abc DO WORD IF Case sizeof 3.不属于C语言关键字的是()。 A.int B.break C.while D.character 4.以下不能定义为用户标示符的是()。 A.scanf B.Void C._3com_ D.int 5.C语言程序的基本单位是()。 A.程序行B.语句C.函数D.字符 6.以下说法中正确的是()。 A.C语言程序总是从第一个定义的函数开始执行 B.在C语言程序中,要调用的函数必须在main( )函数中定义 C.C语言程序总是从main( )函数开始执行 D.C语言程序中的main( )函数必须放在程序的开始部分 7.以下选项中,合法的用户标识符是()。 A.long B._2abc C.3dmax D. 8.已知大写字母A的ASCII码值是65,小写字母a的ASCII码是97,则用八进制表示 的字符常量’\101’是()。 A.字符A B.字符a C.字符c D.非法的常量 9.以下选项中,正确的字符常量是()。 A.”F”B.’\\’’C.’W’D.’’ 10.下列变量定义中合法的是 A.short _a=; B.double b=1+; C.long do=0xfdaL; D.float 2_and=1-e-3; 11.为了避免嵌套的if-else语句的二义性,C语言规定else总是与()组成配对关系。 A.缩排位置相同的if B.在其之前未配对的if C.在其之前未配对的最近的if D.同一行上的if 12.下列运算符中优先级最高的是()。 A.< B.&& C.+ D.!= 13.判断char型变量s是否为小写字母的正确表达式是()。 A.’a’ <= s<=’z’B.(s>=’a’) & (s<=’z’) C.(s>=’a’) && (s<=’z’) D.(’a’<=s) and (’z’>=s)

c语言期末测试题附答案

c语言期末测试题附答 案 集团标准化工作小组 #Q8QGGQT-GX8G08Q8-GNQGJ8-MHHGN#

课程代码:A100002 座位号: 《计算机技术基础(C 语言)》试卷A 姓名: 学号: 专业: 学院: 班级: 20 年 月 日 第一 部分 选择题(共 30 分) 一、单项选择题(本大题共 15 小题,每题只有一个正确答 案,答对一题得 2 分,共 30 分) 1、以下关于C 语言标识符的描述中,正确的是 【 】。 A )标 识符可以由汉字组成 B )标识符只能以字母开头 C )关键字可以作为用户标识符 D )Area 与area 是不同的标识符 2、使下列程序段输出“123,456,78”,键盘输入数据,正确的输入是【 】。 int i,j,k; scanf(“%d,%3d%d”,&i,&j,&k); printf(“%d,%d,%d \n”,i,j,k); 3、判断char 类型的变量c1是否为数字字符的正确表达式为 【 】。 A) (c1>=0)&&(c1<=9) B) (c1>=’0’)&&(c1<=’9’) C) ’0’<=c1<=’9’ D) (c1>=’0’)||(c1<=’9’) 4、若有语句int a=1,b=2,c=3;则以下值为0的表达式是 【 】。 A )’a’&&’b’ B )a<=b C )((a>b)||(b0;i--);后,变量i 的值为 【 】。 A )10 B )9 C )0 D )1 8、若有int *p1, *p2,k; 不正确的语句是 【 】 A. p1=&k B. p2=p1 C. *p1=k+12 D. k=p1+p2 9、在函数中未指定存储类别的局部变量,其隐含的存储类别是 【 】

大学C语言期末考试习题集(带详解答案)

一、单项选择题 1.(A)是构成C语言程序的基本单位。 A、函数 B、过程 C、子程序 D、子例程 2.C语言程序从 C开始执行。 A) 程序中第一条可执行语句 B) 程序中第一个函数 C) 程序中的main函数 D) 包含文件中的第一个函数 3、以下说法中正确的是(C)。 A、C语言程序总是从第一个定义的函数开始执行 B、在C语言程序中,要调用的函数必须在main( )函数中定义 C、C语言程序总是从main( )函数开始执行 D、C语言程序中的main( )函数必须放在程序的开始部分 4.下列关于C语言的说法错误的是(B)。 A) C程序的工作过程是编辑、编译、连接、运行 B) C语言不区分大小写。 C) C程序的三种基本结构是顺序、选择、循环 D) C程序从main函数开始执行 5.下列正确的标识符是(C)。 A.-a1 B.a[i] C.a2_i D.int t 5~8题为相同类型题 考点:标识符的命名规则 (1)只能由字母、数字、下划线构成 (2)数字不能作为标识符的开头 (3)关键字不能作为标识符 选项A中的“-”,选项B中“[”与“]”不满足(1);选项D中的int为关键字,不满足(3) 6.下列C语言用户标识符中合法的是( B)。 A)3ax B)x C)case D)-e2 E)union 选项A中的标识符以数字开头不满足(2);选项C,E均为为关键字,不满足(3);选项D中的“-”不满足(1); 7.下列四组选项中,正确的C语言标识符是(C)。 A) %x B) a+b C) a123 D) 123 选项A中的“%”,选项B中“+”不满足(1);选项D中的标识符以数字开头不满足(2) 8、下列四组字符串中都可以用作C语言程序中的标识符的是(A)。 A、print _3d db8 aBc B、I\am one_half start$it 3pai

实验三 软中断通信

实验三软中断通信 实验目的 1、了解什么是信号 2、熟悉LINUX系统中进程之间软中断通信的基本原理 实验内容 1、编写程序:用fork( )创建两个子进程,再用系统调用signal( )让父进程捕捉键盘上来的中断信号(即按^c键);捕捉到中断信号后,父进程用系统调用kill( )向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止: Child process1 is killed by parent! Child process2 is killed by parent! 父进程等待两个子进程终止后,输出如下的信息后终止: Parent process is killed! 2、分析利用软中断通信实现进程同步的机理 实验指导 一、信号 1、信号的基本概念 每个信号都对应一个正整数常量(称为signal number,即信号编号。定义在系统头文件中),代表同一用户的诸进程之间传送事先约定的信息的类型,用于通知某进程发生了某异常事件。每个进程在运行时,都要通过信号机制来检查是否有信号到达。若有,便中断正在执行的程序,转向与该信号相对应的处理程序,以完成对该事件的处理;处理结束后再返回到原来的断点继续执行。实质上,信号机制是对中断机制的一种模拟,故在早期的UNIX版本中又把它称为软中断。 信号与中断的相似点: (1)采用了相同的异步通信方式; (2)当检测出有信号或中断请求时,都暂停正在执行的程序而转去执行相应的处理程序;(3)都在处理完毕后返回到原来的断点; (4)对信号或中断都可进行屏蔽。 信号与中断的区别: (1)中断有优先级,而信号没有优先级,所有的信号都是平等的; (2)信号处理程序是在用户态下运行的,而中断处理程序是在核心态下运行; (3)中断响应是及时的,而信号响应通常都有较大的时间延迟。 信号机制具有以下三方面的功能: (1)发送信号。发送信号的程序用系统调用kill( )实现; (2)预置对信号的处理方式。接收信号的程序用signal( )来实现对处理方式的预置;(3)收受信号的进程按事先的规定完成对相应事件的处理。 2、信号的发送 信号的发送,是指由发送进程把信号送到指定进程的信号域的某一位上。如果目标进程正在一个可被中断的优先级上睡眠,核心便将它唤醒,发送进程就此结束。一个进程可能在其信号域中有多个位被置位,代表有多种类型的信号到达,但对于一类信号,进程却只能记住其中的某一个。 进程用kill( )向一个进程或一组进程发送一个信号。

c语言期末考试及答案讲解

C语言考试模拟试卷 1.若有定义:char c;int d;程序运行时输入:c=1,d=2<回车>,能把字符1输入给变量c、整数2输入给变量d的输入语句是 A、scanf("c=%d d=%d",&c,&d); B、scanf("c=%c d=%d",&c,&d); C、scanf("c=%d,d=%d",&c,&d); D、scanf("c=%c,d=%d",&c,&d); 【答案】D 【解析】scanf()函数中,%d对应的参数是整数型地址,%c对应参数为char 型地址,C,A选项错误;如果输入地址有多个,应该用逗号隔开,B选项错误,故答案为D选项。 2.以下叙述错误的是 A、在进行模块化程序设计的时候,应首先完成每个模块的编写调试,再集中考虑主程序中的算法 B、同一程序各模块可由一组人员同时进行编写调试,可提高编写程序的效率 C、模块化的程序设计是采用自顶向下、逐步细化的原则 D、程序的每个模块都可通过三种基本结构实现 【答案】A 【解析】结构化程序设计把一个复杂问题的求解过程分阶段进行,需要保证自顶向下、逐步细化、模块化设计、结构化编码。进行模块化设计时,首先设计框架,并定义和调试好各个模块之间的输入输出关系,然后完成各个模块的编写调试后

再集中编译,模块化的程序设计采用自顶向下、逐步细化的原则,A选项叙述错误,C选项叙述正确。各个模块可以由不同人员同时进行编写调试,提高编写程序的效率,B选项叙述正确。结构化程序主要由3种基本控制结构组成,顺序结构、选择结构、循环结构,这三种基本结构可以解决任何复杂的问题,D选项叙述正确。故选择A选项。 3.设有定义:int a=0,b=1,c=1; 以下选项中,表达式值与其它三个不同的是 A、b=a==c B、a=b=c C、a=c==b D、c=a!=c 【答案】A 【解析】赋值运算结合性为由右向左结合,赋值运算符左值为变量,右值为变量或常量,且左右两边数据类型相同才能实现赋值。成功实现赋值后以左值为返回值。逻辑表达式成立则返回1,不成立返回0。A选项逻辑表达式a==c不成立(0),则 b=0,表达式值为0。B选项中将c赋值给b,b=1,再将b赋给a,a=1,表达式值为1。C选项逻辑表达式c==b成立(1),则a=1,表达式值为1。D选项逻辑表达式a!=c成立(1),则c=1,表达式值为1。A选项与其他选项不同,A选项正确。 4.设有两行定义语句:

进程软中断通信资料

进程软中断通信 【预备知识】 进程软中断通信涉及的系统调用描述如下。 1.kill() 进程用kill()向一个进程或一组进程发送一个信号。系统调用格式为int kill(pid,sig)。其中,pid是一个或一组进程的标识符,sig是要发送的软中断信号。信号的发送分如下三种情况。 pid>0时,核心将信号发送给进程pid。 pid=0时,核心将信号发送给与发送进程同组的所用进程。 pid=-1时,核心将信号发送给所有用户标识符真正等于发送进程的有效用户标识号的进程。 2.signal(sig,function) 接收信号的程序用signal()来实现对处理方式的预置,允许调用进程控制软中断信号。系统调用格式为signal(sig function),此时需包含头文件signal.h。其中,sig用于指定信号的类型,sig为0则表示没有收到任何信号,其余类型如表所示。 调用函数使用如下头文件: #include 参数定义如下: signal (sig,function) int sig; void(*func) (); function是该进程中的一个函数地址,在核心返回用户态时,它以软中断信号的序号作为参数调用该函数,对除了信号SIGKILL、SIGTRAP和SIGPWR以外的信号,核心自动重新设置软中断信号处理程序的值为SIG_DFL,进程不能捕获SIGKILL信号。 function的解释如下: (1)function=1时,进程对sig类信号不做任何处理便立即返回,亦即屏蔽该类型号。 (2)function=0时,默认值,进程收到sig信号后终止自己。 (3)function为非0、非1类整数时,执行用户设置的软中断处理程序。

湖南工业大学C语言期末考试复习题(机房题库)

C语言期末考试复习题(机房题库) 2、下列各选项中,合法的c语言关键字是( )。 A. integex B. sin C. string D. void参考答案: D 3、C语言的程序一行写不下时,应该( )。 A. 用回车符换行 B. 在任意一个空格处换行 C. 用分号换行 D. 用逗号换行答案: B 4、以下选项中正确的整型常量是( )。 A. 34.1 B. -80 C. 2,000 D. 1 2 3参考答案: B 5、在C程序中,可以作为用户标识符的一组标识符是( )。 A. void define WORD B. as_b3 _224 Else C. switch _wel ease D. 4b DO SIG答案: B 8、以下叙述不正确的是( ) A. 一个c源程序可由一个或多个函数组成 B. 一个C源程序必须包含一个main()函数 C. c程序的基本组成单位是函数 D. 在C程序中,注释说明只能位于一条语句的后面答案: D 9、C语言规定:在一个源程序中,main()函数的位置( ) A. 必须在最开始 B. 必须在系统调用的库函数的后面 C. 可以任意 D. 必须在最后参考答案: C 10、在C语言中,数字029是一个() A. 八进制数 B. 十六进制数 C. 十进制数 D. 非法数参考答案: D 12、C语言可执行程序的开始执行点是( ) A. 程序中的一条可执行语句 B. 程序中第一个函数 C. 程序中的main函数 D. 包含文件中的第一个函数参考答案: C 13、C程序是( ) A. 顺序执行 B. 从Main函数开始 C. 从函数体开始 D. 任意语句参考答案: B 16、以下说法中正确的是( )。 A. C语言程序中是从第一定义的函数开始执行 B. 在C语言程序中,要调用的函数必须在main()函数中定义 C. C语言程序总是从main()函数开始执行 D. C语言程序中的main()函数必须放在程序的开始部分参考答案: C 17、一个C程序的执行是从( ) A. 本程序的main函数开始,到main函数结束 B. 本程序文件的第一个函数开始,到本程序文件的最后一个函数结束 C. 本程序的main函数开始,到本程序文件的最后一个函数结束 D. 本程序文件的第一个函数开始,到本程序main函数结束参考答案: A 18、以下叙述正确的是( ) A. 在C程序中,main函数必须位于程序的最前面 B. C程序的每行中只能写一条语句 C. C语言本身没有输入输出语句 D. 在对一个C程序进行编译的过程中,可发现注释中的拼写错误答案: C 1、C源程序的基本单位是___函数__

Linux进程通信实验报告

Linux进程通信实验报告 一、实验目的和要求 1.进一步了解对进程控制的系统调用方法。 2.通过进程通信设计达到了解UNIX或Linux系统中进程通信的基本原理。 二、实验内容和原理 1.实验编程,编写程序实现进程的管道通信(设定程序名为pipe.c)。使 用系统调用pipe()建立一条管道线。而父进程从则从管道中读出来自 于两个子进程的信息,显示在屏幕上。要求父进程先接受子进程P1 发来的消息,然后再接受子进程P2发来的消息。 2.可选实验,编制一段程序,使其实现进程的软中断通信(设定程序名为 softint.c)。使用系统调用fork()创建两个子进程,再用系统调用 signal()让父进程捕捉键盘上来的中断信号(即按Del键),当父进程 接受这两个软中断的其中一个后,父进程用系统调用kill()向两个子 进程分别发送整数值为16和17的软中断信号,子进程获得对应软中 断信号后分别输出相应信息后终止。 三、实验环境 一台安装了Red Hat Linux 9操作系统的计算机。 四、实验操作方法和步骤 进入Linux操作系统,利用vi编辑器将程序源代码输入并保存好,然后 打开终端对程序进行编译运行。 五、实验中遇到的问题及解决 六、实验结果及分析 基本实验 可选实验

七、源代码 Pipe.c #include"stdio.h" #include"unistd.h" main(){ int i,j,fd[2]; char S[100]; pipe(fd); if(i=fork==0){ sprintf(S,"child process 1 is sending a message \n"); write(fd[1],S,50); sleep(3); return; } if(j=fork()==0){ sprintf(S,"child process 2 is sending a message \n"); write(fd[1],S,50); sleep(3); return;

大学计算机C语言期末试题A

专业班级:学号:姓名: 一、选择题(用铅笔把选中的方格涂黑40×1=40分) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 二、填空题(25×2=50分) (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13) (14) (15) (16) (17) (18) (19) (20)

(21) (22) (23) (24) (25) 三、编程题(10分) 求10个数的最大公约数和最小公倍数(定义两个函数,求两个数的最大公约数和两个数的最小公倍数)

一、选择题 ( 40×1分=40分) 1.以下叙述中正确的是 (1) 。 A .在编译时可以发现注释中的拼写错误 B . C 语言程序的每一行只能写一条语句 C .main( ){}必须位于程序的开始 D .C 语言程序可以由一个或多个函数组成 2.以下选项中不能用作变量名的是 (2) 。 A ._float B .switch C .sum D ._123 3.已知sizeof(int)的值为2,为将10!的值存储到变量f 中(10!=3628800),变量f 的类 型应声明为 (3) 。 A .long int B .int C .short int D .unsigned int 4. 以下表示中,不能用作C 语言常量的是 (4) 。 A. 0UL B. (long)123 C. 1e0 D.‘\x2a’ 5.数学式 b a 2在C 程序中正确的表示形式为 (5) 。 A .sqrt(a)/2*b B .sqrt(a)/2/b C .sqrt(a)/2b D .sqrt a/(2*b) 6.已知有程序段“char str[][10]={"Aoyun","Beijing"},*p=&str[0][0]; printf("%s\n",p+10);”,则执行printf 语句后输出为 (6) 。 A .Beijing B .Aoyun C .ing D .ng 7.以下声明中错误的是 (7) 。 A .int a[2]; B .int a[]={0,1}; C .int a[3]=0; D .int a[3][4]={0}; 8.以下表示数学式“a=b)&&!(b>=c) D. !(a>=b||b>=c) 9.已有声明“int a=3,b=3,c=2;”,以下选项中值为0的表达式是 (9) 。 A. !a&&!b&&c B. !a||!b||c C. a==b&&b>=c D. a>=b&&b>c 10.若有声明“char ss[8]="Red";”,则sizeof(ss)的值是 (10) 。 A .8 B .4 C .3 D .1 11. 以下程序运行后的输出结果是 (11) 。 A. 2 2 B. 7 2 C. 7 5 D. 6 2 main() { char a[7]="a0\0a0\0"; int i,j; i=sizeof(a); j=strlen(a); printf("%d %d",i,j); } 12. 下面关于循环语句for 、while 、do_while 的叙述中,正确的是 (12) 。 A. 三种循环语句的循环体都必须放入一对花括号中 B. 三种循环语句中都可以缺省循环终止条件表达式 C. 三种循环语句的循环体都至少被无条件地执行一次

C语言期末考试试题及答案

个人收集整理-仅供参考 2008-2009学年第一学期期末考试试卷 考试说明:本课程为闭卷考试,可携带书写与修正文具,满分为:100 分. 考试结束后请将后页答题卡与试卷分开上交 ..... 一、单选题<每题2分,共30分) 1.以下叙述正确地是 ______ . A)C程序地每行只能写一条语句 B>语言本身没有输入输出语句 C)在C程序中,注释说明只能位于一条语句地后面 D)在多函数地程序中,main函数必须放在其它子函数地定义之前 2.以下不正确地常量表示形式是 ______ . A> 0.45 B>0XF5 C>‘\85’D> 32.67E-5 3. 以下不正确地变量名是 ______ . A)R&B B> _max C>INT D> SUM3 4. 以下正确地一组语句是 ________. A> int x=y=5; B> int n; scanf("%d",&n>;int a[n]; C> char a,*p; p=&a; D> char s[10]; s="hello"; 5. 若以下变量均已正确定义和赋值,则正确地语句是 ________. A> a=b==5; B> y=x%2.0; C> x+y=10; D> n=8=2*4; 6.下面各组中与给出地程序段功能不等价地是 ________. if(a>0> b=1; else if(a==0> b=0; else b=-1; A>if(a>0> b=1; B>if(a>0> b=1; C>if(a>0> b=1; D>if(a>=0> if(a==0> b=0; else if(a==0> b=0; if(a>0> b=1; if(a<0> b=-1; if(a<0> b=-1; else b=-1; if(a==0> b=0; else b=0; if(a<0> b=-1; 7. 运行下面程序段,若输入abc#,则程序输出是:________. char c;int v1=0,v2=0; while((c=getchar(>>!='#'> { switch(c> { case 'a':v1++; default :v1++;v2++; case 'c':v2++; } } printf("v1=%d,v2=%d\n",v1,v2>; A>2,2 B>3,5 C> 3,4 D>2,5

(精选)大学大一C语言程序设计期末考试试卷和答案

四川师范大学计算机科学学院 2012~2013学年度第二学期期末考试 C 语言程序设计试卷 试卷A 答卷说明: 1、 本试卷共6页,答题纸3页,4个大题,35个小题,满分100分,120分钟完卷。 2、 闭卷考试,所有的答案都必须写在答题纸上,判卷以答题纸上答案为准。 3、 本试卷适用于2011级1、2、3班。 一、 单项选择题(在每小题的四个备选答案中,选出一个正确答案,并将正确答案的 序号填在题干的括号内。每小题1分,共20分) 1、 在C 语言中,下列标识符中合法的是( ) A). -int B). in1_3 C). A_B!D D). const 2、 不属于C 语言基本数据类型的是( ) A). int B). union C). char D). unsigned 3、 下面是对宏定义的描述。不正确的是( ) A). 宏不存在类型问题,宏名无类型,它的参数也无类型 B). 宏替换不占用运行时间 C). 宏替换只是字符串的简单替代 D). 宏替换时先检查类型,类型一致时才可以替换 4、 C 语言中,″\\xfds ″在内存中占用的字节数是( ) A).3 B).4 C).5 D).6 5、 设int x;则执行printf(″%x \n″,~x^x);的结果是( ) A). ffffffff B). ff __________________学院__________级___________班 姓名_______________ 学号_______________ ………………………………(密)………………………………(封)………………………………(线)……………………………… 密 封 线 内 答 题 无 效

实验一 进程管理

实验一进程管理 1. 实验目的 ⑴加深对进程概念的理解,明确进程和程序的区别; ⑵进一步认识并发执行的实质; ⑶分析进程争用资源的现象,学习解决进程互斥的方法; ⑷了解Linux系统中进程通信的基本原理。 2. 实验准备 ⑴阅读Linux的sched.h源码文件,加深对进程管理的理解。 ⑵阅读Linux的fork.h源码文件,分析进程的创建过程。 3. 实验内容 ⑴进程的创建 编写一段程序,使用系统调用fork ( )创建两个子进程。当此程序运行时,在系统中有一个父进程和两个子进程活动。让每一个进程在屏幕上显示一个字符:父进程显示字符“a”;子进程显示字符“b”和字符“c”。试观察记录屏幕上的显示结果,并分析原因。 ⑵进程的控制 修改已编写的程序,将每个进程输出一个字符改为每个进程输出一句话,再观察程序执行时屏幕上出现的现象,并分析原因。 如果在程序中使用系统调用lockf ( )来给每一个进程加锁,可以实现进程之间的互斥,观察并分析出现的现象。 ⑶软中断通信 编制一段程序实现进程的软中断通信。要求:使用系统调用fork ( )创建两个子进程,再用系统调用signal( )让父进程捕捉键盘上发来的中断信号(既按Del键);当捕捉到中断信号后,父进程系统调用kill( )向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止:Child process 1 is killed by parent! Child process 2 is killed by parent! 父进程等待两个子进程终止后,输出如下的信息后终止: Parent process is killed! 在上面的程序中增加语句signal (SIGINT, SIG_IGN) 和signal (SIGQUIT, SIG_IGN),观察执行结果,并分析原因。 4. 实验指导

江苏大学大一c语言期末复习题汇总

选择题 1.下列字符序列中,不可用作C语言标识符的是()。 A.abc123 B.no.1 C._123_ D._ok 2.请选出可用作C语言用户标识符的一组标识符()。 A.void B.a3_b3 C.For D.2a define _123 -abc DO WORD IF Case sizeof 3.不属于C语言关键字的是()。 A.int B.break C.while D.character 4.以下不能定义为用户标示符的是()。 A.scanf B.V oid C._3com_ D.int 5.C语言程序的基本单位是()。 A.程序行B.语句C.函数D.字符 6.以下说法中正确的是()。 A.C语言程序总是从第一个定义的函数开始执行 B.在C语言程序中,要调用的函数必须在main( )函数中定义 C.C语言程序总是从main( )函数开始执行 D.C语言程序中的main( )函数必须放在程序的开始部分 7.以下选项中,合法的用户标识符是()。 A.long B._2abc C.3dmax D.A.dat 8.已知大写字母A的ASCII码值是65,小写字母a的ASCII码是97,则用八进制表示 的字符常量’\101’是()。 A.字符A B.字符a C.字符c D.非法的常量 9.以下选项中,正确的字符常量是()。 A.”F”B.’\\’’C.’W’D.’’ 10.下列变量定义中合法的是 A.short _a=1-.le-1; B.double b=1+5e2.5; C.long do=0xfdaL; D.float 2_and=1-e-3; 11.为了避免嵌套的if-else语句的二义性,C语言规定else总是与()组成配对关系。 A.缩排位置相同的if B.在其之前未配对的if C.在其之前未配对的最近的if D.同一行上的if 12.下列运算符中优先级最高的是()。 A.< B.&& C.+ D.!= 13.判断char型变量s是否为小写字母的正确表达式是()。 A.’a’ <= s<=’z’B.(s>=’a’) & (s<=’z’) C.(s>=’a’) && (s<=’z’) D.(’a’<=s) and (’z’>=s) 14.已知x=45, y=’a’, z=0; 则表达式(x>=z && y<’z’ || !y)的值是()。 A.0 B.语法错 C.1 D.“假”

C语言期末考试复习题及答案

C语言期末考试复习题及答案 一、选择题:下列各题A)、B)、C)、D)四个选项中只有一个是正 确的,请将正确的选项涂写在答案纸上。答在试卷上不得分。 (1)C语言规定:在一个源程序中,main函数的位置 D 。 A)必须在最后B)必须在系统调用的库函数的后面。 C)必须在最开始。。D)可以任意 (2) C语言中的标识符只能由字母、数字和下划线三种字符组成,且第一个字符 A 。 A)必须为字母或下划线。。B)必须为下划线。 C)必须为字母D)可以是字母、数字和下划线中的任一种字符。 (3)下面四个选项中,均是正确的八进制数或十六进制数的选项是 B 。 A)-10 0x8f -011 B) 010 -0x11 0xf1 C) 0abc -017 0xc D) 0a12 -0x123 -0xa (4) C语言中int型数据在内存中占两个字节,则unsegned int取值范围是 A 。 A)0 ~ 65535 B)0 ~ 32767 C)-32767 ~ 32768 D)-32768 ~ 327687 (5) 若有定义:int a = 7; floa x = , y = ; 则表达式x + a % 3 * (int) (x + y) % 2/4 的值是 D 。 A) B) 0.00000 C) D) (6)已知ch是字符型变量,下面不正确的赋值语句是 B 。 A)ch = 5 + 9 ; B) ch= ' a + b '; C) ch = ' \ 0 '; D) ch= '7' + '6' ; (7) 设x , y和z是int型变量,且x = 3, y = 4 , z = 5 则下面表达式中值为0的

实验5 进程间通信实验

实验五进程间通信实验 一、实验目的 1、了解什么是信号。 2、熟悉LINUX系统中进程之间软中断通信的基本原理。 3、了解什么是管道 4、熟悉UNIX/LINUX支持的管道通信方式 二、实验内容 1、编写一段程序,使用系统调用fork( )创建两个子进程,再用系统调用signal( )让父进程捕捉键盘上来的中断信号(即按ctrl+c键),当捕捉到中断信号后,父进程用系统调用kill( )向两个子进程发出信号,子进程捕捉到信号后,分别输出下列信息后终止:Child process 1 is killed by parent! Child process 2 is killed by parent! 父进程等待两个子进程终止后,输出以下信息后终止: Parent process is killed! <参考程序> #include #include #include #include #include int wait_mark; void waiting(),stop(); void main() {int p1, p2; signal(SIGINT,stop); while((p1=fork())==-1); if(p1>0) /*在父进程中*/ { while((p2=fork())==-1); If(p2>0) /*在父进程中*/ { wait_mark=1; waiting(0); kill(p1,10); kill(p2,12); wait( ); wait( ); printf("parent process is killed!\n"); exit(0); } else /*在子进程2中*/ { wait_mark=1; signal(12,stop); waiting();

2019年C语言期末考试题及答案

的一个)。, 求最小值出现的位置(如果最小值1、给定n个数据例如:输入的数组为:求出第一次出现的位置即可)。出现多次, 1 2 3 4 1 5 6 12 18 9k+1i<=n最大值第三行五行 10 11 2的值并输出。计算公式为:2、编写程序求无理数e e=1+1/1!+1/2!+1/3!+......+1/n! 求出的最大数为18,行坐标为2,列坐标为1。。当1/n!<0.000001时e=2.718282 7、求一个n位自然数的各位数字的积。(n 是小于10的 自然数)求一批数中最大值和最小值的积。3、 2、某一正数的值保留位小数,对第三位进行四舍48、计算n门课程的平均值,计算结果作为函数值返回。五入。例如:若有5门课程的成绩是:92,76,69,58,88,

则函数的值为76.599998。 x,5、从键盘上输入任意实数求出其所对应的函数值。 z=(x-4)的二次幂(x>4) 9、求一批数中小于平均值的数的个数。 z=x的八次幂(x>-4) z=z=4/(x*(x+1))(x>-10)z=|x|+20(其他) M×N求出、6整型数组的最大元素及其所在的行坐标10、编写函数判断一个整数m的各位数字之和能否被7整除,及列坐标(如果最大元素不唯一,选择位置在最前面 0。调用该函数找出可以被7整除则返回1,否则返回 100~200之间满足条件的所有数。 15、实现两个整数的交换。 例如:给a和b分别输入:60和65,输入为:a=65 b=60 void fun(int tt[M][N],int pp[N]),11、请编一个函数16、将字符串中的小写字母转换为对应的大写字母, N列的二维数组,求出二维数组每列M tt指向一个行其它字符不变。

上海海事大学(C语言期末)上机题库

试卷编号:9688 所属语言:C语言 试卷方案:期中考试 试卷总分:100分 共有题型:5种 一、填空共15题(共计15分) 第1题(1.0分)题号:528 设a、b、c为整型数, 且a=2、b=3、c=4, 则执行完以下语句: a*=16+(b++)-(++c); 后,a的值是【1】. 答案: =======(答案1)======= 28 第2题(1.0分)题号:78 已知 i=5;写出语句 i+=012; 执行后整型变量 i 的十进制值是【1】. 答案: =======(答案1)======= 15 第3题(1.0分)题号:510 若a是int型变量,则计算表达式 a=25/3%3 后a的值为【1】. 答案: =======(答案1)======= 2 第4题(1.0分)题号:437 以下程序的输出结果为【1】, #include "stdio.h" main(){int a=010,j=10;printf("%d,%d\n",++a,j--);}

答案: =======(答案1)======= 9,10 第5题(1.0分)题号:431 执行下面两个语句,输出的结果是【1】,char c1=97,c2=98;printf("%d %c",c1,c2); 答案: =======(答案1)======= 97 b *第6题(1.0分)题号:293 getchar()函数只能接收一个【1】. 答案: =======(答案1)======= 字符 第7题(1.0分)题号:440 设a=3,b=4,c=5,则表达式!(a+b)+c-1&&b+c/2的值为【1】. 答案: =======(答案1)======= 1 第8题(1.0分)题号:95 已知a=13,b=6, a&&b的十进制数值为【1】. 答案: =======(答案1)======= 1 第9题(1.0分)题号:306 当a=1,b=2,c=3时,执行以下程序段后b=【1】.

大一c语言期末试题及参考答案word版本

2004级信息学院《C语言设计》考试试题 一、判断下列语句或程序的对错。 10分√ 1 int x=y=z=’0’; (×) y,z没有定义 2 #include ; (×)不能有分号,#开头的结尾均不能有分号; 3 printf(“%s\n”,”c language”); (√) 4 float a[100]; int *p=a; (×)数据类型不匹配 5 char str[20]; 6 int data[4]={0,1,2,3,4}; (×)五个元素,但是只有四个单元 7 float x=1.45e+310L; (×)数值越界 8 int xyz-1=2; (×) 9 int x=‘\xae’ ; (√) 10 int *p,a[2][3] ; p=a ; (×)数据类型不匹配 二计算下列表达式的值 10分 设 unsigned int a=10,b=17,c=5,d=3; float f ; (1)f=b/c ( 3.0 ) (2)!(a+b)+c-1&&b+c/2 ( 1 ) (3)(a^b)+(c>>1+d) ( 0x1b ) (4)a+=b%=a=b ( 17 ) (5)a=2,b=a*++b ( 2 ) 三程序改错 10分 (1)求两个浮点数的平方和及平方差 #include float calculate (float x,float y,float *sub);添加函数原型声明 main () { float a,b; float add_reasult, sub_result; scanf (“%f,%f”,a,b); add_result=calculate(a,b,&sub_result); printf( “a*a+b*b=%d,a*a-b*b=%d\n”,add_result,sub_result); } float calculate (float x,float y,float *sub) 添加函数类型 { float *temp; 应该直接定义为变量float temp; sub=a*a-b*b ; *sub=a*a-b*b;

安徽大学C语言期末试卷(含答案),DOC

安徽大学 C语言程序设计期末试题 一. 单项选择题:(每题2分,共20分) 1.执行程序: int a; printf("%d \n", (a=3*5, a*4, a+5) );后,a的值是______。 A)60 B)20 C)15 D)10 2.程序段:int x=023; printf("%d \n", --x );的输出是______。 A)19 B)18 C)23 D)22 3.程序段:static char s[10]={'C','h','i','n','a'}; printf("%d\n",strlen(s));的输出是______。 A)6 B)5 C)11 D)10 4.若fun( int x) { printf(“%d\n”,x);}, 则fun_______。 A)返回值为void 类型B)返回值类型无法确定 C)返回值为int 类型D)没有返回值 5. 有宏定义:#define MULT1(a,b) a*b #define MULT2(a,b) (a)*(b) 且引用宏:y=MULT1(3+2,5+8); z=MULT2(3+2,5+8); 则y和z的值是______。 A)y=65,z=65 B)y=21,z=65 C)y=65,z=21 D)y=21,z=21 6.程序段char s[]="Hello", *p; p=s;执行后,*(p+5)的值是______。 A)'o' B)'\0' C)'o'的地址D)不确定的值 7. 程序段:char *s=”ABCD”, *p; for( p=s; *p ; p++) printf("%s\n", p);的输出是______。 A)ABCD B)A C)D D)ABCD BCD B C ABC CD C B AB D D A A 8. 若定义:int (*p)[3]; 则标识符p______。 A)定义不合法。 B)是一个指针数组名,每个元素是一个指向整型变量的指针。 C)是一个指针,它指向一个具有三个元素的一维数组。 D)是一个指向整型变量的指针。 9. 定义: struct sk {int a; float b;} data,*p; 且p=&data; 则正确引用成员a的方式是______。 A) p.data.a B) (*p).a C) p->data.a D) (*p).data.a 10. 以二进制只读方式打开一个已有的文本文件file1,正确调用fopen函数的方式是______。 A)FILE *fp; B)FILE *fp; fp=fopen("file1","rb"); fp=fopen("file1","r+"); C)FILE *fp; D)FILE *fp; fp=fopen("file1","r"); fp=fopen("file1","rb+");

大学生C语言期末考试必做题库

C语言

第一题 在屏幕上显示一个短句“Programming in C is fun!” #include int main() { printf("Programming in C is fun!\n"); return 0; } 第二题 在屏幕上显示如下网格。 +---+---+ | | | | | | +---+---+ #include int main( ) { printf("+---+---+\n| | |\n| | |\n+---+---+\n"); return 0; } 第三题 在屏幕上显示一条语句“This is a C program.” #include int main() { printf("This is a C program.\n"); return 0; } 第四题 输出如下图形: ********** very good! ********** #include int main( ) { printf("**********\nvery good!\n**********\n"); return 0; }

第一题求平方根(基础) 程序填空,不要改变与输入输出有关的语句。 输入1个实数x,计算并输出其平方根(保留1位小数)。 例:输入17输出The square root of 17.0 is 4.1 #include #include int main( ) { double x, root; scanf("%lf", &x); root=sqrt(x); printf("The square root of %0.1f is %0.1f\n", x, root); return 0; } 第二题计算一个三位数的个十百位之和 编写程序。 输入一个三位数(大于0),计算这个三位数的个位、十位、百位数字之和. 例如:输入:152输出:sum=8 #include #include int main() { int n,a,b,c,sum; scanf("%d",&n); a=n/100; b=n%100/10; c=n%10; sum=a+b+c; printf("sum=%d\n",sum); return 0; } 第三题(公共)求圆的周长、面积及圆球体积 程序填空,不要改变与输入输出有关的语句。 求以r为半径的圆周长c和圆面积s,再求以r为半径的圆球体积v。(已知r=3.67在程序中直接赋值,pi的值为3.1415926) 按照下列形式输出: printf("c=%f\ns=%f\nv=%f\n",c,s,v); #include int main( ) { double r,c,s,v,pi=3.1415926; r=3.67; c=2*pi*r;

相关主题
文本预览
相关文档 最新文档