Вы находитесь на странице: 1из 27

Operating system 1. What are the features of RTOS which are responsible for its real time nature?

reliability, predictability, performance, compactness, and scalability.


2.

Linux schedules the parent and child processes independently; theres no guarantee of which one will run first, or how long it will run before Linux interrupts it and lets the other process (or some other process on the system) run. In particular, none, part, or all of the ls command may run in the child process before the parent completes.2 Linux promises that each process will run eventuallyno process will be completely starved of execution resources. You may specify that a process is less importantand should be given a lower priority by assigning it a higher niceness value. By default, every process has a niceness of zero. A higher niceness value means that the process is given a lesser execution priority; conversely, a process with a lower (that is, negative) niceness gets more execution time.

What is the scheduling procedure in rtos or linux?

3. How the scheduler gets a chance to run?


Ans:) From RTC ISR

4. What are the resources shared across threads? Ans:) all threads in a single program share the same

address space.This means that if one thread modifies a location in memory (for instance, a global variable), the change is visible to all other threads.This allows multiple threads to operate on the same data without the use interprocess communication mechanisms.
Each thread has its own call stack, however.This allows each thread to execute different code and to call and return from subroutines in the usual way.

fork a child process.The child process is initially running its parents program, with its parents virtual memory, file descriptors, and so on copied.The child process can modify its memory, close file descriptors, and the like without affecting its parent, and vice versa.When a program creates another thread, though, nothing is copied.The creating and the created thread share the same memory space, file descriptors, and other system resources as the original. If one thread changes the value of a variable, for instance, the other thread subsequently will see the modified value. Similarly, if one thread closes a file descriptor, other threads may not read

5. How multiprocessing is different from multithreading?

For some programs that benefit from concurrency, the decision whether to use processes or threads can be difficult. Here are some guidelines to help you decide which concurrency model best suits your program: All threads in a program must run the same executable.A child process, on the other hand, may run a different executable by calling an exec function. An errant thread can harm other threads in the same process because threads share the same virtual memory space and other resources. For instance, a wild memory write through an uninitialized pointer in one thread can corrupt memory visible to another thread. An errant process, on the other hand, cannot do so because each process has a copy of the programs memory space. Copying memory for a new process adds an additional performance overhead relative to creating a new thread. However, the copy is performed only when the memory is changed, so the penalty is minimal if the child process only reads memory. Threads should be used for programs that need fine-grained parallelism. For example, if a problem can be broken into multiple, nearly identical tasks, threads may be a good choice. Processes should be used for programs that need coarser parallelism. Sharing data among threads is trivial because threads share the same memory. (However, great care must be taken to avoid race conditions, as described previously.) Sharing data among processes requires the use of IPC mechanisms.This can be more cumbersome but makes multiple processes less likely to suffer from concurrency bugs.
n n n n n

6. Mention different IPC techniques? What are the design implications in each of them? Semaphores Message Queues Signals Sockets 7. Which is the fastest IPC mechanism? Ans: ) Shared memory
Shared memory is the fastest form of interprocess communication because all

processes share the same piece of memory. Access to this shared memory is as fast as accessing a processs nonshared memory, and it does not require a system call or entry to the kernel. It also avoids copying data unnecessarily. Because the kernel does not synchronize accesses to shared memory, you must provide your own synchronization. For example, a process should not read from the memory until after data is written there, and two processes must not write to the same memory location at the same time.A common strategy to avoid these race conditions is to use semaphores

8. What are sockets? Can we use them for inter-task communication internal to a processor? Yes
VxWorks provides standard BSD socket calls, which allow real-time VxWorks tasks and other processes to communicate in any combination with each other over the network. Any task can open one or more sockets, to which other sockets can be connected. Data written to one socket of a connected pair is read, transparently, from the other socket. Because of this transparency, the two tasks do not necessarily know whether they are communicating with another process or VxWorks task on the same CPU or on another CPU, or with a process running under some other host operating system.
A socket is a bidirectional communication device that can be used to communicate with another process on the same machine or with a process running on other machines. Sockets are the only interprocess communication well discuss in this chapter that permit communication between processes on different computers. Internet programs such as Telnet, rlogin, FTP, talk, and the World Wide Web use sockets. When you create a socket, you must specify three parameters: communication style, namespace, and protocol. A communication style controls how the socket treats transmitted data and specifies the number of communication partners.When data is sent through a socket, it is packaged into chunks called packets.The communication style determines how these

packets are handled and how they are addressed from the sender to the receiver. Connection styles guarantee delivery of all packets in the order they were sent. If packets are lost or reordered by problems in the network, the receiver automatically requests their retransmission from the sender. A connection-style socket is like a telephone call:The addresses of the sender and receiver are fixed at the beginning of the communication when the connection is established. Datagram styles do not guarantee delivery or arrival order. Packets may be lost or reordered in transit due to network errors or other conditions. Each packet must be labeled with its destination and is not guaranteed to be delivered.The system guarantees only best effort, so packets may disappear or arrive in a different order than shipping. A datagram-style socket behaves more like postal mail.The sender specifies the receivers address for each individual message. A socket namespace specifies how socket addresses are written. A socket address identifies one end of a socket connection. For example, socket addresses in the local namespace are ordinary filenames. In Internet namespace, a socket address is composed of the Internet address (also known as an Internet Protocol address or IP address) of a host attached to the network and a port number.The port number distinguishes among multiple sockets on the same host. A protocol specifies how data is transmitted. Some protocols are TCP/IP, the primary networking protocols used by the Internet; the AppleTalk network protocol; and the UNIX local communication protocol. Not all combinations of styles, namespaces, and protocols are supported.
n n

9. How can we provide protected access to shared memory? MutualExclusion 10. How can we avoid priority inversion? Priority Inheritance Priority Ceiling 11. What are semaphores? How binary semaphore is different from mutex?
A semaphore is used to control access to the critical section and a shared resource.

12. What are the different criteria to select different types of semaphores? 13. What are spin locks and when are they used? Ans:) Suppose that you write a thread function that executes a loop infinitely,
performing some work on each iteration.The thread loop, however, needs to be controlled by a flag:The loop runs only when the flag is set; when the flag is not set, the loop pauses. During each iteration of the loop, the thread function checks that the flag is set. Because the flag is accessed by multiple threads, it is protected by a mutex.This implementation may be correct, but it is not efficient.The thread function will spend lots of CPU

whenever the flag is not set, checking and rechecking the flag, each time locking and unlocking the mutex.What you really want is a way to put the thread to sleep when the flag is not set, until some circumstance changes that might cause the flag to become set.

14.

To spawn a new process, you first use fork to make a copy of the current process.Then you use exec to transform one of these processes into an instance of the program you want to spawn. When a program calls fork, a duplicate process, called the child process, is created.The parent process continues executing the program from the point that fork was called. The child process, too, executes the same program from the same place. Parent process id will be non zero and child process id will be Zero. A common pattern to run a subprogram within a program is first to fork the process and then exec the subprogram.This allows the calling program to continue execution in the parent process while the calling program is replaced by the subprogram in the child process.

What is the procedure to create process in linux?

15. What is the use of kernel mode and user mode in RTOS context? 16. What is the context of interrupts? 17. Explain the interrupt mechanism with control flow. CPU/processor will have Interrupt Input pin, peripheral controller will have Interrupt output pin. Request line will connect both of them. Whenever there is any interrupt signal by any device, the peripheral control will present interrupt number to the interrupt controller. On interrupt controller all of this interrupt ports will be hardwired with priorities. CPU will use those interrupt number to directly get the corresponding ISR. Normally all ISRs are vectored in known location. Whenever there is any interrupt signal, the CPU pushes current instruction to the stack for the return to the previous instruction execution point. Then CPU adds interrupt address as a offset to the known memory location, where the ISR is located. This instruction is loaded into instruction pointer. 18. Explain interrupt priorities. All interrupt priorities are hardwired on interrupt controller. Interrupt controller can be on-chip with processor or external to the processor. The main intention of priorities is to serve higher priority interrupt incase of multiple interrupts raised. 19. What are soft and hard watchdogs? How are they used in system design?

A watchdog timer is based on a counter that counts down from some initial value to zero. The embedded software selects the counter's initial value and periodically restarts it. If the counter ever reaches zero before the software restarts it, the software is presumed to be malfunctioning and the processor's reset signal is asserted. Soft watchdogs: Using monitor task. This scheme uses a task dedicated to the watchdog. This task wakes up at a regular interval and checks the sanity of all other tasks in the system. If all tasks pass the test, the watchdog is kicked. The watchdog monitor task runs at a higher priority than the tasks it is monitoring. The watchdog timeout can be chosen to be the maximum time during which all regular tasks have had a chance to run from their start point through one full loop back to their start point again. Each task has a flag which can have two values, ALIVE and UNKNOWN. The flag is later read and written by the monitor. The monitor's job is to wake up before the watchdog timeout expires and check the status of each flag. If all flags contain the value ALIVE, every task got its turn to execute and the watchdog may be kicked. Some tasks may have executed several loops and set their flag to ALIVE several times, which is acceptable. After kicking the watchdog, the monitor sets all of the flags to UNKNOWN. By the time the monitor task executes again, all of the UNKNOWN flags should have been overwritten with ALIVE. Figure 3 shows an example with three tasks.

Hardware Implementation:
A simple example is shown in Listing 1. Here we have a single infinite loop that controls the entire behavior of the system. This software architecture is common in many embedded systems with low-end processors and behaviors based on a single operational frequency. The hardware implementation of this watchdog allows the counter value to be set via a memory-mapped register.

Listing 1: Kicking the dog


uint16 volatile * pWatchdog = (uint16 volatile *) 0xFF0000; main(void) { hwinit(); for (;;) { *pWatchdog = 10000; read_sensors(); control_motor(); display_status(); }

Suppose that the loop must execute at least once every five milliseconds. (Say the motor must be fed new control parameters at least that often.) If the watchdog timer's counter is initialized to a value that corresponds to five milliseconds of elapsed time, say 10,000, and the software has no bugs, the watchdog timer will never expire; the software will always restart the counter before it reaches zero.

What is the trigger to enter kernel mode? Ans:) . A process invokes a system call.
20.

The CPU executing the process signals an exception, which is some unusual condition such as an invalid instruction. The kernel handles the exception on behalf of the process that caused it. A peripheral device issues an interrupt signal to the CPU to notify it of an event such as a request for attention, a status change, or the completion of an I/O operation. Each interrupt signal is dealt by a kernel program called an interrupt handler. Since peripheral devices operate asynchronously with respect to the CPU, interrupts occur at unpredictable times.

21. 22.

Explain virtual memory concept. Explain paging mechanism.

Networking 1. 2. 3. 4. Explain end to end packet flow over a network. Explain layer 2 forwarding. What is ARP, proxy etc... Explain Ethernet and ATM headers.

Ans: Preamble | SFD | DMA | SMA | Length/Type | data | FCS

5. How Ethernet is different from ATM. 6. Explain virtual LAN. 7. Why 0 and 4095 are reserved in VLAN. Ans: 4095 is a Discard VLNS 8. What is VLAN stacking? 9. How forwarding decision table is maintained for each vlan. 10. Explain routing.

11. Explain IP header. 12. Explain static and dynamic routing protocols. 13. Explain ospf and rip protocols. 14. Explain RIB. 15. How a layer 2/3 switch can decide whether to route a packet or to forward it. 16. How the packet is sent when the destination host is not in the local network? 17. What happens when default gateway is not configured? 18. What is network byte order? Big endian order (I Gusess, should be confirmed by google) 19. Explain TCP and UDP headers. 20. What are the uses of TCP? 21. If the packet length is small, how it will be handled? 22. What is the basis for ATM? 23. PC-PC packet transmission (eg FTP)? 24. Explain TCP,UDP? Difference? 25. Difference RIP and OSPF? (He expected Classful masking) 26.

C Programming and data structures What are the differences between a macro and inline? Macro doesnt check datatype check errors. Inline functions throws errors if there is mismatch of datatypes in function arguments. 2. What is a volatile variable? The usual four examples of volatile objects are: An object that is a memory-mapped I/O port An object that is shared between multiple concurrent processes An object that is modified by an asynchronous signal handler An automatic storage duration object declared in a function that calls setjmp, and whose value is changed between the call to setjmp and a corresponding call to longjmp 3. Are all global variables volatile? No 4. What is a static function? Function has file Scope. It cant be called from outside the file 5. What is a function pointer and what are its uses? Callback functions, Signals, ISR 6. Explain union of structures. 7. What are the disadvantages of BST? 8. What are the design criteria for trees? 9. Explain balanced trees. 10. What are the different regions in memory? 11. Lifetime and scope of the variables.
1.

Auto block scope, destroyed after block exit extern program scope, memory persists untill the program ends static scope to the block, memory persists untill the program ends register processor register, local to block 12. What is sizeof(). Unary operator 1. Write a program to swap adjacent elements of a singly linked list. void swap_adjacent_elements1(SL *del_node) { SL *node = head, *prev = head; printf("void swap_adjacent_elements1(SL *del_node)\n"); while(node->next) { if(del_node == head) { SL *temp = head->next->next; node->next->next = head; head = node->next; node->next = temp; break; } else if(node == del_node) { SL *temp = node->next->next; prev->next = node->next; prev->next->next = node; node->next = temp; break; } prev = node; node = node->next; }

} 2. Write a program to delete an element of linked list without traversing and not using temporary pointer. Node *temp = NULL; if(node == head) { *temp = head; head = node->next; } else if(node->next != NULL) { memcpy(&(node->data),&(node->node->next>data),sizeof(node->data)); memcpy(node->next,node->next->next,sizeof(Node)); } if(temp !=NULL) { free(temp); temp = NULL; } if(node && node->prev && node->next) { node->prev->next = node->next; node->next->prev = node->prev; } else if(node == head) { head = node->next; node->next->prev = NULL; } elseif(node->next == NULL) { node->prev->next = NULL; }

free(node); node = NULL; 3. Write a program to store the order of elements coming out of an infinite stream [1, N] without repetitions. 4. Write an efficient program to search an element in 2D matrix which is sorted both horizontally and vertically.
For example the 2D array could look like the following 1456 2579

search diagonally. diag 1 = 1 diag2 = 2,4 ?. for example given the number 7 we take the bounds of each diagonal and check wether the number will fall in that range else move farward. here diagonal selection is sequential but the number search is binary. sort on the last column suppose n*n matrix low= 0; high =n-1; while ( low!=high ) { mid = floor [( low + high )/2];

if(a[mid][n-1]==item) return if(a[mid][n-1]>item) high = mid; if(a[mid][n-1]<item) low= mid+1; } apply binary search on 1d array left 5. Write a program to compare two binary trees. int compare(BNode *node1,BNode *node2) { int flag1 = 0, flag2 =0; if(node1 && node2 && node1 == node2) { flag1 = compare(node1->left_link,node2->left_link); if(flag1 == 1) { return 1; } flag2 = compare(node1->right_link,node2->right_link); if(flag2 == 1) { return 1; } printf("Success till end\n"); return 0; } else if(!node1 && !node2) { return 0; } else { return 1;

} } 6. Write a program to reverse a string without using temporary char array. main() { char str[20] = "satish"; int i = 0,n = strlen(str)-1; while(i<=n) { str[i] = str[i] ^ str[n]; str[n] = str[i] ^ str[n]; str[i] = str[i] ^ str[n]; i++; n--; } printf("%s",str); getch(); } 7. Write a standard library function which will take packet version and provides different functionalities based on version and it should not be modified in future if new packet versions are introduced. 8. Write a program to implement binary tree. 9. Write an efficient sorting program. 10. Write a program to implement hashing. 11. Write a macro which gives the size of any variable. 12. Write a macro to swap the nibbles. main() { unsigned char a = 91; unsigned char b = ((a<<4) & 0xf0) | ((a>>4)& 0x0f); //char b = (1<<4 & a) | (1>>4 & a); printf("%d",b); getch();

}
13. Write

a program to remove redundant dots in a string without using temp string. /*The following is easy to remember*/ main() { char a[] = "sat...pqr...st.uv.yyy...z."; int i = 0,j = 0; while(a[i]) { if(a[i] == '.' && a[i] == a[i+1]) { i++; } else { a[j] = a[i]; j++; i++; } } a[j] = '\0'; printf("%s",a); getch(); } -----------------------------------------main() { char a[] = "sat..in..dia.....over...alu...bike.create..onetwo"; int i = 0, j =0, flag =0; while(a[i]) { if(a[i] == '.') { if(flag == 0)

{ a[j++] = a[i]; flag = 1; } i++; } else { a[j++] = a[i++]; flag = 0; } } a[j] = '\0'; printf("%s",a); getch(); } 14. Write state machine program.

typedef enum sat_IDLE_DAY_STATES_NUM { idle_state =0, sleepy_mode, funny_mode, sad_mode, busy_mode, NUM_STATES }sat_IDLE_DAY_STATES_NUM; typedef enum sat_IDLE_DAY_EVENTS_NUM { ignore_evt = 0, sat_day_beginning_alarm, sat_phone_rings, sat_pending_work_reminder, emergency_at_home,

Television_started, NUM_EVENTS }sat_IDLE_DAY_EVENTS_NUM; typedef struct sat_fsm_info { sat_IDLE_DAY_EVENTS_NUM prev_evt; sat_IDLE_DAY_EVENTS_NUM curr_evt; sat_IDLE_DAY_STATES_NUM prev_state; sat_IDLE_DAY_STATES_NUM curr_state; }sat_fsm_info; void wake_up(sat_fsm_info*); void ignore(sat_fsm_info*); void shut_alarm(sat_fsm_info*); void shut_phone(sat_fsm_info*); void offhook(sat_fsm_info*); void mute(sat_fsm_info*); void takeup_act(sat_fsm_info*); //Sat FSM - idle day void (*sat_FSM [NUM_STATES] [NUM_EVENTS]) (sat_fsm_info*) = { {wake_up,offhook,shut_alarm,ignore}, {shut_phone,offhook,mute,offhook}, {wake_up,ignore,shut_alarm,takeup_act}, {wake_up,ignore,shut_alarm,ignore}, {wake_up,ignore,shut_alarm,ignore}, }; main() { int evt; sat_fsm_info *sat_fsm_data = (sat_fsm_info*)malloc(sizeof(sat_fsm_info));

memset(sat_fsm_data,0,sizeof(sat_fsm_info)); do { printf("Which event is going to happen and tell me your current state\n"); printf(" CURRENT EVENT \n"); printf("1::Wake-up Alarm \n"); printf("2::Phone Ringing \n"); printf("3::Pending Work Reminder \n"); printf("4::Emergency at home \n"); printf("5::Television started \n"); scanf("%d",&evt); sat_fsm_data->prev_evt = sat_fsm_data->curr_evt; sat_fsm_data->prev_state = sat_fsm_data->curr_state; sat_fsm_data->curr_evt = evt; printf("Current state is %d Cureent evt is %d\n",sat_fsm_data>curr_state,evt); sat_FSM[sat_fsm_data->curr_state][evt](sat_fsm_data); }while(evt!=0); //getch(); } void wake_up(sat_fsm_info *data) { printf("We are in wake_up state\n\n\n\n\n"); data->curr_state=0; } void ignore(sat_fsm_info *data) { printf("We are in ignore state\n\n\n\n\n"); } void shut_phone(sat_fsm_info *data) {

printf("We are in shut_phone state\n\n\n\n\n"); } void offhook(sat_fsm_info *data) { printf("We are in offhook() handler\n\n\n\n\n"); data->curr_state = busy_mode; } void mute(sat_fsm_info *data) { printf("We are in mute state\n\n\n\n\n"); } void shut_alarm(sat_fsm_info *data) { printf("We are in shut_alarm state\n\n\n\n\n"); } void takeup_act(sat_fsm_info *data) { printf("We are in takeup_act state\n\n\n\n\n"); } 15. Write a program for avl tree. 16. Write a efficient program to transmit information of different things present in a box. 17. Write a program to count number of 1s in a byte. main() { int num = 0; int x = num,count=0; while(x) { if(x & 0x1) { count++; } x >>=1; }

printf("count=%d\n",count); getch(); } 18. Write a program to reverse an integer. #include<stdio.h> #include<conio.h> main() { int temp,num,rnum=0; printf("\nEnter the decimal number , num = "); scanf("%d",&num); printf("\n The Decimal Number entered is = %d , ",num); while(num>0) { temp = num % 10; rnum = 10 * rnum + temp; num = num /10; } printf("\n Reverse of Decimal num = %d ",rnum); getch(); } 19. Write a program to print A for 1, Bfor 2, AA for 27 and so on based on the integer input. #define MAX_ALPHABET 26 #define ASCII_OFFSET 64 main() { int i=0,j=0,k=0; printf("Enter no \n"); scanf("%d",&i); j=i/MAX_ALPHABET; k=i%MAX_ALPHABET; if (j){

printf("\n%c",j+ASCII_OFFSET); printf("%c",k+ASCII_OFFSET); }else{ printf("\n%c",k+ASCII_OFFSET); } getch(); } 20. Write a macro to multiply two numbers. ANS:) #define MUL(a,b) ((a) * (b)) 21. Write a program to implement printf. 22. Write a program to print ; using it only once. 23. Find redundant value in a linked list? void find_redundant() { SL *temp1 = head; int flag = 0; while(temp1) { SL *temp2 = temp1->next; while(temp2) { if(temp1->data == temp2->data) { flag = 1; break; } temp2 = temp2->next; } if(flag == 1) { printf("found\n"); break; } temp1 = temp1->next; }

} void remove_redundant() { SL *node1 = head; SL *node2 = NULL; SL *prev = NULL; while(node1) { node2 = node1->next; prev = node1; while(node2) { if(node1->data == node2->data) { prev->next = node2->next; free(node2); node2 = prev; } prev = node2; node2 = node2->next; } node1 = node1->next; } } 24. In BST, find a node , if data is not found return next higher variable? 25. In BST, find a node, if data is found return data+1? 26. Implement dictionary using tree? 27. Find out middle node in a linked list? void find_middle_node() { SL *temp1=head,*temp2 = head; int count = 0; while(temp1 && temp2)

{ temp1 = temp1->next; if(temp2->next) { temp2 = temp2->next->next; } else { break; } count++; } printf("the mid node = %x data= %d, position= %d\n",temp1,temp1->data,count); } 28. BST Definition? 29. Recursion eg? 30. disadvantages of recursion in Embedded systems? 31. Explain pointer to pointer with an example? 32. reentrant functions?

Output for code snippets 1 char a[]={1,2,3,4}; int *p; p=(int *)a; printf(%d\n,*p); Ans:) based on little/big end machine 2. int *p=0; p++;

printf(%d\n,p); Ans:) 4 3. char a[]=print; for(int i=0;i<20;i--) printf(%s,a); Ans) print infinite times 4. int i=6; if(5==i) printf(equal); else printf(not equal); Ans) equal

Design and debugging 1. Linked list corruption. 2. Stack corruption. 3. Heap corruption. 4. Crash in an inline function. 5. Inter card communication. 6. Protected shared memory access. 7. Zombie state. 8. Memory utilization. 9. Race over condition. 10. Deadlocks. Product oriented 1. Network processor. 2. Redundant Cpu design. 3. Fault tolerant system. 4. Traffic loss. 5. Packet switching. 6. Protocol stack. 7. Address mappings over different n/w technologies. 8. Redundant traffic paths. 9. IMA. 10. Data path

C++ programming

Вам также может понравиться