Google Interview Question for Software Engineer / Developers


Country: England
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
4
of 4 vote

Process
Each process provides the resources needed to execute a program. A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution. Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads.

Thread
A thread is the entity within a process that can be scheduled for execution. All threads of a process share its virtual address space and system resources. In addition, each thread maintains exception handlers, a scheduling priority, thread local storage, a unique thread identifier, and a set of structures the system will use to save the thread context until it is scheduled. The thread context includes the thread's set of machine registers, the kernel stack, a thread environment block, and a user stack in the address space of the thread's process. Threads can also have their own security context, which can be used for impersonating clients.

Found this on MSDN here: h t t p : / / msdn.microsoft.com/en-us/library/ms681917(VS.85).aspx

- Sam February 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

> What is a process and what is a thread?

A process is a part of a program; it has its own execution context, including access rights, memory, open file handles, an own process id and that process id of its parent/creator.
Processes can share memory. If their parent process gets killed, they will be killed, too; if the parent just terminates, the new parent will be the init process.

There are two different kinds of threads: The "kernel thread" is a lightweight process that shares at least its memory with other threads, which makes it a part of the same running program. The "user thread" is a lightweight thread, which can run with other user threads in the same kernel thread. User threads are a design by libraries and programming languages, and are not part of the operating system.

Processes contain kernel threads, which contain user threads.
Processes do not contain other processes but can create child processes.
The first kernel thread /is/ the process, others are some kind of child processes.

User threads are self-sheduled, they have to switch voluntarily, iff they run in the same kernel thread; but they can be designed to be able to migrate between kernel threads. Processes and kernel threads are sheduled by the kernel and can run in parallel on multicore cpus or even on different machines.


> How do you prevent deadlocks?

By designing thought-out, probably using
spin locks, other locks, atomic operations, and STM (Software Transactional Memory) mechanisms.

- comonad February 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Contention can occur when two threads inside a program are trying to share a single shared resource, like a buffer or some flag variables. This is prevented by locking mechanism.

Deadlock occurs when one thread is contending for resource held by another thread, which is contending for resource held by the first thread, such that there is a circular chain of dependency. Deadlock can be prevented by taking locks in a particular order. If each thread wants to take more than one locks at any time, it should do so in a pre-defined sequence. This will prevent deadlocks.

- Gaurav Kukreja February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good one, I agree on that: locks should be acquired in a particular order for *all* threads to prevent deadlocks.

btw, another common "deadlock pattern" is waiting on a semaphore while holding a mutex, e.g. consider the following:

thread1:
mutex.lock();
sem.wait();
// do some work
mutex.unlock();

thread2:
mutex.lock();
sem.signal();
mutex.unlock();

suppose semaphone 'sem' is initially set to '0'.
Then depending on the order of execution of threads,
it might happen that thread 1 acquires mutex first and hence the semaphore will never be signalled..

- 111 April 04, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More