Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9004777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:58:29+00:00 2026-06-16T00:58:29+00:00

When creating sub-processes in java using Runtime.exec() , I am aware that I have

  • 0

When creating sub-processes in java using Runtime.exec(), I am aware that I have to fill the input/drain the output streams to prevent blocking of the subprocess.

Interestingly, the javadoc of Process states a little bit more:

...failure to promptly write the input stream or read the output stream of
the subprocess may cause the subprocess to block, and even deadlock.

I am wondering that in this situation the subprocess can also deadlock!

Questions:
1. Under which conditions does it deadlock?
2. Why does it deadlock?
3. Can you provide a short example program which shows this deadlock?
4. Is this deadlock a bug in the OS?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:58:30+00:00Added an answer on June 16, 2026 at 12:58 am

    Deadlock can occur due to limited buffer sizes when the parent tries to send too much data to the input stream of its child before reading any of the output.

    Consider this code:

    final int LINES = 10;
    // "tr" is a Unix command that translates characters;
    // Here, for every line it reads, it will output a line with
    // every 'a' character converted to 'A'.  But any command that outputs
    // something for every line it reads (like 'cat') will work here
    Process p = Runtime.getRuntime().exec("tr a A");
    Writer out = new OutputStreamWriter(p.getOutputStream());
    for (int i = 0; i < LINES; i++) {
        out.write("abcdefghijklmnopqrstuvwxyz\n");
    }
    out.close();
    // Read all the output from the process and write it to stdout
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
    

    For small values for lines, it will work fine; all of tr‘s output can fit in the OS buffer before we start reading it. However, for large values (>10000 ought to be sufficient), the OS buffer will fill up; within the tr command, calls to write will block, waiting for the buffer to be drained, and in turn, the buffer that the Java code is writing to will fill up (because tr is be blocked, preventing it from reading from its input), in turn blocking our calls to out.write, leading to a deadlock where both processes are waiting to write into full buffers that aren’t being actively read from.

    This deadlock is not a bug in the OS, as limited buffer sizes for inter-process communication are a deliberate design decision. The alternative (unlimited buffer sizes) has some downsides:

    • Can exhaust kernel memory
    • To avoid the above, if buffers automatically “spill over” to disk, that can lead to unpredictable performance and can potentially fill up the disk.

    As an aside, deadlock can also occur due to in-process buffers. Suppose that, to attempt to address the deadlock above, we changed our Java code to write one line, then read one line, alternatingly. However, it’s common for Linux processes to not flush after every line when they’re not writing directly to a terminal. So tr might read a line, and write it to its libc output buffer, and than block waiting for the next line to be written–and our Java code will block waiting for tr to output a line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nested object that is a type of User (say Sub). I
I'm creating a sub theme for Omega and am using a book for hierarchical
When using python-daemon , I'm creating subprocesses likeso: import multiprocessing class Worker(multiprocessing.Process): def __init__(self,
I've two routes, 1) creating sub-tables for each user and storing his individual content
I'm creating a sub dialog another vendor is calling. I'm having trouble reading the
When creating reports I find myself frequently joining to ad-hock sub queries like this:
Creating a class at runtime is done as follows: klass = Class.new superclass, &block
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
I'm in the process of creating a Backbone.js app using Require.js. Each view file
Brief Explanation I am unsure about the structure that I have used for this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.